kernel - change tsleep related idents to const volatile void *
[dragonfly.git] / sbin / quotacheck / quotacheck.c
blobfc4b555e987201760d8322ce3e410dbd2f6e8a92
1 /*
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
10 * are met:
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)quotacheck.c 8.3 (Berkeley) 1/29/94
38 * $FreeBSD: src/sbin/quotacheck/quotacheck.c,v 1.11 1999/08/28 00:14:01 peter Exp $
39 * $DragonFly: src/sbin/quotacheck/quotacheck.c,v 1.10 2006/10/19 21:22:13 pavalos Exp $
43 * Fix up / report on disk quotas & usage
45 #include <sys/param.h>
46 #include <sys/stat.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/quota.h>
50 #include <vfs/ufs/fs.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <fstab.h>
56 #include <grp.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
63 const char *qfname = QUOTAFILENAME;
64 const char *qfextension[] = INITQFNAMES;
65 const char *quotagroup = QUOTAGROUP;
67 union {
68 struct fs sblk;
69 char dummy[MAXBSIZE];
70 } un;
71 #define sblock un.sblk
72 long dev_bsize = 1;
73 ino_t maxino;
75 struct quotaname {
76 long flags;
77 char grpqfname[MAXPATHLEN + 1];
78 char usrqfname[MAXPATHLEN + 1];
80 #define HASUSR 1
81 #define HASGRP 2
83 struct fileusage {
84 struct fileusage *fu_next;
85 u_long fu_curinodes;
86 u_long fu_curblocks;
87 u_long fu_id;
88 char fu_name[1];
89 /* actually bigger */
91 #define FUHASH 1024 /* must be power of two */
92 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
94 int aflag; /* all file systems */
95 int gflag; /* check group quotas */
96 int uflag; /* check user quotas */
97 int vflag; /* verbose */
98 int fi; /* open disk file descriptor */
99 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
101 struct fileusage *
102 addid(u_long, int, char *);
103 char *blockcheck(char *);
104 void bread(daddr_t, char *, long);
105 extern int checkfstab(int, int, void * (*)(struct fstab *),
106 int (*)(char *, char *, struct quotaname *));
107 int chkquota(char *, char *, struct quotaname *);
108 void freeinodebuf(void);
109 struct ufs1_dinode *
110 getnextinode(ino_t);
111 int getquotagid(void);
112 int hasquota(struct fstab *, int, char **);
113 struct fileusage *
114 lookup(u_long, int);
115 void *needchk(struct fstab *);
116 int oneof(char *, char*[], int);
117 void resetinodebuf(void);
118 int update(char *, char *, int);
119 void usage(void);
122 main(int argc, char **argv)
124 struct fstab *fs;
125 struct passwd *pw;
126 struct group *gr;
127 struct quotaname *auxdata;
128 int i, argnum, maxrun, errs;
129 long done = 0;
130 char ch, *name;
132 errs = maxrun = 0;
133 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
134 switch(ch) {
135 case 'a':
136 aflag++;
137 break;
138 case 'g':
139 gflag++;
140 break;
141 case 'u':
142 uflag++;
143 break;
144 case 'v':
145 vflag++;
146 break;
147 case 'l':
148 maxrun = atoi(optarg);
149 break;
150 default:
151 usage();
154 argc -= optind;
155 argv += optind;
156 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
157 usage();
158 if (!gflag && !uflag) {
159 gflag++;
160 uflag++;
162 if (gflag) {
163 setgrent();
164 while ((gr = getgrent()) != NULL)
165 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
166 endgrent();
168 if (uflag) {
169 setpwent();
170 while ((pw = getpwent()) != NULL)
171 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
172 endpwent();
174 if (aflag)
175 exit(checkfstab(1, maxrun, needchk, chkquota));
176 if (setfsent() == 0)
177 errx(1, "%s: can't open", FSTAB);
178 while ((fs = getfsent()) != NULL) {
179 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
180 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
181 (auxdata = needchk(fs)) &&
182 (name = blockcheck(fs->fs_spec))) {
183 done |= 1 << argnum;
184 errs += chkquota(name, fs->fs_file, auxdata);
187 endfsent();
188 for (i = 0; i < argc; i++)
189 if ((done & (1 << i)) == 0)
190 fprintf(stderr, "%s not found in %s\n",
191 argv[i], FSTAB);
192 exit(errs);
195 void
196 usage(void)
198 fprintf(stderr, "%s\n%s\n",
199 "usage: quotacheck -a [-guv]",
200 " quotacheck [-guv] filesys ...");
201 exit(1);
204 void *
205 needchk(struct fstab *fs)
207 struct quotaname *qnp;
208 char *qfnp;
210 if (strcmp(fs->fs_vfstype, "ufs") ||
211 strcmp(fs->fs_type, FSTAB_RW))
212 return (NULL);
213 if ((qnp = malloc(sizeof(*qnp))) == NULL)
214 errx(1, "malloc failed");
215 qnp->flags = 0;
216 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
217 strcpy(qnp->grpqfname, qfnp);
218 qnp->flags |= HASGRP;
220 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
221 strcpy(qnp->usrqfname, qfnp);
222 qnp->flags |= HASUSR;
224 if (qnp->flags)
225 return (qnp);
226 free(qnp);
227 return (NULL);
231 * Scan the specified filesystem to check quota(s) present on it.
234 chkquota(char *fsname, char *mntpt, struct quotaname *qnp)
236 struct fileusage *fup;
237 struct ufs1_dinode *dp;
238 int cg, i, mode, errs = 0;
239 ino_t ino;
241 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
242 warn("%s", fsname);
243 return (1);
245 if (vflag) {
246 printf("*** Checking ");
247 if (qnp->flags & HASUSR)
248 printf("%s%s", qfextension[USRQUOTA],
249 (qnp->flags & HASGRP) ? " and " : "");
250 if (qnp->flags & HASGRP)
251 printf("%s", qfextension[GRPQUOTA]);
252 printf(" quotas for %s (%s)\n", fsname, mntpt);
254 sync();
255 dev_bsize = 1;
256 bread(SBOFF, (char *)&sblock, (long)SBSIZE);
257 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
258 maxino = sblock.fs_ncg * sblock.fs_ipg;
259 resetinodebuf();
260 for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
261 for (i = 0; i < sblock.fs_ipg; i++, ino++) {
262 if (ino < ROOTINO)
263 continue;
264 if ((dp = getnextinode(ino)) == NULL)
265 continue;
266 if ((mode = dp->di_mode & IFMT) == 0)
267 continue;
268 if (qnp->flags & HASGRP) {
269 fup = addid((u_long)dp->di_gid, GRPQUOTA,
270 NULL);
271 fup->fu_curinodes++;
272 if (mode == IFREG || mode == IFDIR ||
273 mode == IFLNK)
274 fup->fu_curblocks += dp->di_blocks;
276 if (qnp->flags & HASUSR) {
277 fup = addid((u_long)dp->di_uid, USRQUOTA,
278 NULL);
279 fup->fu_curinodes++;
280 if (mode == IFREG || mode == IFDIR ||
281 mode == IFLNK)
282 fup->fu_curblocks += dp->di_blocks;
286 freeinodebuf();
287 if (qnp->flags & HASUSR)
288 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
289 if (qnp->flags & HASGRP)
290 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
291 close(fi);
292 return (errs);
296 * Update a specified quota file.
299 update(char *fsname, char *quotafile, int type)
301 struct fileusage *fup;
302 FILE *qfi, *qfo;
303 u_long id, lastid;
304 off_t offset;
305 struct ufs_dqblk dqbuf;
306 static int warned = 0;
307 static struct ufs_dqblk zerodqbuf;
308 static struct fileusage zerofileusage;
310 if ((qfo = fopen(quotafile, "r+")) == NULL) {
311 if (errno == ENOENT)
312 qfo = fopen(quotafile, "w+");
313 if (qfo) {
314 warnx("creating quota file %s", quotafile);
315 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
316 fchown(fileno(qfo), getuid(), getquotagid());
317 fchmod(fileno(qfo), MODE);
318 } else {
319 warn("%s", quotafile);
320 return (1);
323 if ((qfi = fopen(quotafile, "r")) == NULL) {
324 warn("%s", quotafile);
325 fclose(qfo);
326 return (1);
328 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
329 errno == EOPNOTSUPP && !warned && vflag) {
330 warned++;
331 printf("*** Warning: %s\n",
332 "Quotas are not compiled into this kernel");
334 for (lastid = highid[type], id = 0, offset = 0; id <= lastid;
335 id++, offset += sizeof(struct ufs_dqblk)) {
336 if (fread((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfi) == 0)
337 dqbuf = zerodqbuf;
338 if ((fup = lookup(id, type)) == 0)
339 fup = &zerofileusage;
340 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
341 dqbuf.dqb_curblocks == fup->fu_curblocks) {
342 fup->fu_curinodes = 0;
343 fup->fu_curblocks = 0;
344 continue;
346 if (vflag) {
347 if (aflag)
348 printf("%s: ", fsname);
349 printf("%-8s fixed:", fup->fu_name);
350 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
351 printf("\tinodes %lu -> %lu",
352 (u_long)dqbuf.dqb_curinodes,
353 (u_long)fup->fu_curinodes);
354 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
355 printf("\tblocks %lu -> %lu",
356 (u_long)dqbuf.dqb_curblocks,
357 (u_long)fup->fu_curblocks);
358 printf("\n");
361 * Reset time limit if have a soft limit and were
362 * previously under it, but are now over it.
364 if (dqbuf.dqb_bsoftlimit &&
365 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
366 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
367 dqbuf.dqb_btime = 0;
368 if (dqbuf.dqb_isoftlimit &&
369 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
370 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
371 dqbuf.dqb_itime = 0;
372 dqbuf.dqb_curinodes = fup->fu_curinodes;
373 dqbuf.dqb_curblocks = fup->fu_curblocks;
374 if (fseek(qfo, (long)offset, SEEK_SET) < 0) {
375 warn("%s: seek failed", quotafile);
376 return(1);
378 fwrite((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfo);
379 quotactl(fsname, QCMD(Q_SETUSE, type), id,
380 (caddr_t)&dqbuf);
381 fup->fu_curinodes = 0;
382 fup->fu_curblocks = 0;
384 fclose(qfi);
385 fflush(qfo);
386 ftruncate(fileno(qfo),
387 (off_t)((highid[type] + 1) * sizeof(struct ufs_dqblk)));
388 fclose(qfo);
389 return (0);
393 * Check to see if target appears in list of size cnt.
396 oneof(char *target, char **list, int cnt)
398 int i;
400 for (i = 0; i < cnt; i++)
401 if (strcmp(target, list[i]) == 0)
402 return (i);
403 return (-1);
407 * Determine the group identifier for quota files.
410 getquotagid(void)
412 struct group *gr;
414 if ((gr = getgrnam(quotagroup)) != NULL)
415 return (gr->gr_gid);
416 return (-1);
420 * Check to see if a particular quota is to be enabled.
423 hasquota(struct fstab *fs, int type, char **qfnamep)
425 char *opt;
426 char *cp;
427 static char initname, usrname[100], grpname[100];
428 static char buf[BUFSIZ];
430 if (!initname) {
431 snprintf(usrname, sizeof(usrname),
432 "%s%s", qfextension[USRQUOTA], qfname);
433 snprintf(grpname, sizeof(grpname),
434 "%s%s", qfextension[GRPQUOTA], qfname);
435 initname = 1;
437 strcpy(buf, fs->fs_mntops);
438 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
439 if ((cp = strchr(opt, '=')) != NULL)
440 *cp++ = '\0';
441 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
442 break;
443 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
444 break;
446 if (!opt)
447 return (0);
448 if (cp)
449 *qfnamep = cp;
450 else {
451 snprintf(buf, sizeof(buf),
452 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
453 *qfnamep = buf;
455 return (1);
459 * Routines to manage the file usage table.
461 * Lookup an id of a specific type.
463 struct fileusage *
464 lookup(u_long id, int type)
466 struct fileusage *fup;
468 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
469 if (fup->fu_id == id)
470 return (fup);
471 return (NULL);
475 * Add a new file usage id if it does not already exist.
477 struct fileusage *
478 addid(u_long id, int type, char *name)
480 struct fileusage *fup, **fhp;
481 int len;
483 if ((fup = lookup(id, type)) != NULL)
484 return (fup);
485 if (name)
486 len = strlen(name);
487 else
488 len = 10;
489 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
490 errx(1, "calloc failed");
491 fhp = &fuhead[type][id & (FUHASH - 1)];
492 fup->fu_next = *fhp;
493 *fhp = fup;
494 fup->fu_id = id;
495 if (id > highid[type])
496 highid[type] = id;
497 if (name)
498 bcopy(name, fup->fu_name, len + 1);
499 else {
500 sprintf(fup->fu_name, "%lu", id);
501 if (vflag)
502 printf("unknown %cid: %lu\n",
503 type == USRQUOTA ? 'u' : 'g', id);
505 return (fup);
509 * Special purpose version of ginode used to optimize pass
510 * over all the inodes in numerical order.
512 ino_t nextino, lastinum;
513 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
514 struct ufs1_dinode *inodebuf;
515 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
517 struct ufs1_dinode *
518 getnextinode(ino_t inumber)
520 long size;
521 daddr_t dblk;
522 static struct ufs1_dinode *dp;
524 if (inumber != nextino++ || inumber > maxino) {
525 errx(1, "bad inode number %ju to nextinode",
526 (intmax_t)inumber);
528 if (inumber >= lastinum) {
529 readcnt++;
530 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
531 if (readcnt % readpercg == 0) {
532 size = partialsize;
533 lastinum += partialcnt;
534 } else {
535 size = inobufsize;
536 lastinum += fullcnt;
538 bread(dblk, (char *)inodebuf, size);
539 dp = inodebuf;
541 return (dp++);
545 * Prepare to scan a set of inodes.
547 void
548 resetinodebuf(void)
551 nextino = 0;
552 lastinum = 0;
553 readcnt = 0;
554 inobufsize = blkroundup(&sblock, INOBUFSIZE);
555 fullcnt = inobufsize / sizeof(struct ufs1_dinode);
556 readpercg = sblock.fs_ipg / fullcnt;
557 partialcnt = sblock.fs_ipg % fullcnt;
558 partialsize = partialcnt * sizeof(struct ufs1_dinode);
559 if (partialcnt != 0) {
560 readpercg++;
561 } else {
562 partialcnt = fullcnt;
563 partialsize = inobufsize;
565 if (inodebuf == NULL &&
566 (inodebuf = malloc((u_int)inobufsize)) == NULL)
567 errx(1, "malloc failed");
568 while (nextino < ROOTINO)
569 getnextinode(nextino);
573 * Free up data structures used to scan inodes.
575 void
576 freeinodebuf(void)
579 if (inodebuf != NULL)
580 free(inodebuf);
581 inodebuf = NULL;
585 * Read specified disk blocks.
587 void
588 bread(daddr_t bno, char *buf, long cnt)
591 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
592 read(fi, buf, cnt) != cnt)
593 errx(1, "block %ld", (long)bno);