]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/quotacheck/quotacheck.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / quotacheck / quotacheck.c
1 /*
2  * Copyright (c) 1980, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Elz at The University of Melbourne.
7  *
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  * 4. 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #if 0
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 static char sccsid[] = "@(#)quotacheck.c        8.3 (Berkeley) 1/29/94";
42 #endif /* not lint */
43 #endif
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 /*
48  * Fix up / report on disk quotas & usage
49  */
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ffs/fs.h>
58
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <fstab.h>
63 #include <grp.h>
64 #include <pwd.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 #include "quotacheck.h"
71
72 char *qfname = QUOTAFILENAME;
73 char *qfextension[] = INITQFNAMES;
74 char *quotagroup = QUOTAGROUP;
75
76 union {
77         struct  fs      sblk;
78         char    dummy[MAXBSIZE];
79 } sb_un;
80 #define sblock  sb_un.sblk
81 union {
82         struct  cg      cgblk;
83         char    dummy[MAXBSIZE];
84 } cg_un;
85 #define cgblk   cg_un.cgblk
86 long dev_bsize = 1;
87 ino_t maxino;
88
89 union dinode {
90         struct ufs1_dinode dp1;
91         struct ufs2_dinode dp2;
92 };
93 #define DIP(dp, field) \
94         ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
95         (dp)->dp1.field : (dp)->dp2.field)
96
97 #define HASUSR  1
98 #define HASGRP  2
99
100 struct fileusage {
101         struct  fileusage *fu_next;
102         u_long  fu_curinodes;
103         u_long  fu_curblocks;
104         u_long  fu_id;
105         char    fu_name[1];
106         /* actually bigger */
107 };
108 #define FUHASH 1024     /* must be power of two */
109 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
110
111 int     aflag;                  /* all file systems */
112 int     gflag;                  /* check group quotas */
113 int     uflag;                  /* check user quotas */
114 int     vflag;                  /* verbose */
115 int     fi;                     /* open disk file descriptor */
116
117 struct fileusage *
118          addid(u_long, int, char *, char *);
119 char    *blockcheck(char *);
120 void     bread(ufs2_daddr_t, char *, long);
121 void     freeinodebuf(void);
122 union dinode *
123          getnextinode(ino_t);
124 int      getquotagid(void);
125 int      hasquota(struct fstab *, int, char **);
126 struct fileusage *
127          lookup(u_long, int);
128 struct quotaname *needchk(struct fstab *);
129 int      oneof(char *, char*[], int);
130 void     printchanges(char *, int, struct dqblk *, struct fileusage *, u_long);
131 void     setinodebuf(ino_t);
132 int      update(char *, char *, int);
133 void     usage(void);
134
135 int
136 main(int argc, char *argv[])
137 {
138         struct fstab *fs;
139         struct passwd *pw;
140         struct group *gr;
141         struct quotaname *qnp;
142         int i, argnum, maxrun, errs, ch;
143         long done = 0;
144         char *name;
145
146         errs = maxrun = 0;
147         while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
148                 switch(ch) {
149                 case 'a':
150                         aflag++;
151                         break;
152                 case 'g':
153                         gflag++;
154                         break;
155                 case 'u':
156                         uflag++;
157                         break;
158                 case 'v':
159                         vflag++;
160                         break;
161                 case 'l':
162                         maxrun = atoi(optarg);
163                         break;
164                 default:
165                         usage();
166                 }
167         }
168         argc -= optind;
169         argv += optind;
170         if ((argc == 0 && !aflag) || (argc > 0 && aflag))
171                 usage();
172         if (!gflag && !uflag) {
173                 gflag++;
174                 uflag++;
175         }
176         if (gflag) {
177                 setgrent();
178                 while ((gr = getgrent()) != NULL)
179                         (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
180                             NULL);
181                 endgrent();
182         }
183         if (uflag) {
184                 setpwent();
185                 while ((pw = getpwent()) != NULL)
186                         (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
187                             NULL);
188                 endpwent();
189         }
190         /*
191          * The maxrun (-l) option is now deprecated.
192          */
193         if (maxrun > 0)
194                 warnx("the -l option is now deprecated");
195         if (aflag)
196                 exit(checkfstab());
197         if (setfsent() == 0)
198                 errx(1, "%s: can't open", FSTAB);
199         while ((fs = getfsent()) != NULL) {
200                 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
201                     (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
202                     (qnp = needchk(fs)) &&
203                     (name = blockcheck(fs->fs_spec))) {
204                         done |= 1 << argnum;
205                         errs += chkquota(name, fs->fs_file, qnp);
206                 }
207         }
208         endfsent();
209         for (i = 0; i < argc; i++)
210                 if ((done & (1 << i)) == 0)
211                         fprintf(stderr, "%s not found in %s\n",
212                                 argv[i], FSTAB);
213         exit(errs);
214 }
215
216 void
217 usage(void)
218 {
219         (void)fprintf(stderr, "%s\n%s\n",
220                 "usage: quotacheck [-guv] [-l maxrun] -a",
221                 "       quotacheck [-guv] filesystem ...");
222         exit(1);
223 }
224
225 struct quotaname *
226 needchk(struct fstab *fs)
227 {
228         struct quotaname *qnp;
229         char *qfnp;
230
231         if (strcmp(fs->fs_vfstype, "ufs") ||
232             strcmp(fs->fs_type, FSTAB_RW))
233                 return (NULL);
234         if ((qnp = malloc(sizeof(*qnp))) == NULL)
235                 errx(1, "malloc failed");
236         qnp->flags = 0;
237         if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
238                 strcpy(qnp->grpqfname, qfnp);
239                 qnp->flags |= HASGRP;
240         }
241         if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
242                 strcpy(qnp->usrqfname, qfnp);
243                 qnp->flags |= HASUSR;
244         }
245         if (qnp->flags)
246                 return (qnp);
247         free(qnp);
248         return (NULL);
249 }
250
251 /*
252  * Possible superblock locations ordered from most to least likely.
253  */
254 static int sblock_try[] = SBLOCKSEARCH;
255
256 /*
257  * Scan the specified file system to check quota(s) present on it.
258  */
259 int
260 chkquota(char *fsname, char *mntpt, struct quotaname *qnp)
261 {
262         struct fileusage *fup;
263         union dinode *dp;
264         int cg, i, mode, errs = 0;
265         ino_t ino, inosused, userino = 0, groupino = 0;
266         dev_t dev, userdev = 0, groupdev = 0;
267         char *cp;
268         struct stat sb;
269
270         if (qnp == NULL)
271                 err(1, "null quota information passed to chkquota()\n");
272         if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
273                 warn("%s", fsname);
274                 return (1);
275         }
276         if ((stat(mntpt, &sb)) < 0) {
277                 warn("%s", mntpt);
278                 return (1);
279         }
280         dev = sb.st_dev;
281         if (vflag) {
282                 (void)printf("*** Checking ");
283                 if (qnp->flags & HASUSR)
284                         (void)printf("%s%s", qfextension[USRQUOTA],
285                             (qnp->flags & HASGRP) ? " and " : "");
286                 if (qnp->flags & HASGRP)
287                         (void)printf("%s", qfextension[GRPQUOTA]);
288                 (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
289         }
290         if (qnp->flags & HASUSR) {
291                 if (stat(qnp->usrqfname, &sb) == 0) {
292                         userino = sb.st_ino;
293                         userdev = sb.st_dev;
294                 }
295         }
296         if (qnp->flags & HASGRP) {
297                 if (stat(qnp->grpqfname, &sb) == 0) {
298                         groupino = sb.st_ino;
299                         groupdev = sb.st_dev;
300                 }
301         }
302         sync();
303         dev_bsize = 1;
304         for (i = 0; sblock_try[i] != -1; i++) {
305                 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
306                 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
307                      (sblock.fs_magic == FS_UFS2_MAGIC &&
308                       sblock.fs_sblockloc == sblock_try[i])) &&
309                     sblock.fs_bsize <= MAXBSIZE &&
310                     sblock.fs_bsize >= sizeof(struct fs))
311                         break;
312         }
313         if (sblock_try[i] == -1) {
314                 warn("Cannot find file system superblock");
315                 return (1);
316         }
317         dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
318         maxino = sblock.fs_ncg * sblock.fs_ipg;
319         for (cg = 0; cg < sblock.fs_ncg; cg++) {
320                 ino = cg * sblock.fs_ipg;
321                 setinodebuf(ino);
322                 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
323                     sblock.fs_cgsize);
324                 if (sblock.fs_magic == FS_UFS2_MAGIC)
325                         inosused = cgblk.cg_initediblk;
326                 else
327                         inosused = sblock.fs_ipg;
328                 /*
329                  * If we are using soft updates, then we can trust the
330                  * cylinder group inode allocation maps to tell us which
331                  * inodes are allocated. We will scan the used inode map
332                  * to find the inodes that are really in use, and then
333                  * read only those inodes in from disk.
334                  */
335                 if (sblock.fs_flags & FS_DOSOFTDEP) {
336                         if (!cg_chkmagic(&cgblk))
337                                 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
338                         cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
339                         for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
340                                 if (*cp == 0)
341                                         continue;
342                                 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
343                                         if (*cp & i)
344                                                 break;
345                                         inosused--;
346                                 }
347                                 break;
348                         }
349                         if (inosused <= 0)
350                                 continue;
351                 }
352                 for (i = 0; i < inosused; i++, ino++) {
353                         if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
354                             (mode = DIP(dp, di_mode) & IFMT) == 0)
355                                 continue;
356                         /*
357                          * XXX: Do not account for UIDs or GIDs that appear
358                          * to be negative to prevent generating 100GB+
359                          * quota files.
360                          */
361                         if ((int)DIP(dp, di_uid) < 0 ||
362                             (int)DIP(dp, di_gid) < 0) {
363                                 if (vflag) {
364                                         if (aflag)
365                                                 (void)printf("%s: ", mntpt);
366                         (void)printf("out of range UID/GID (%u/%u) ino=%u\n",
367                                             DIP(dp, di_uid), DIP(dp,di_gid),
368                                             ino);
369                                 }
370                                 continue;
371                         }
372
373                         /*
374                          * Do not account for file system snapshot files
375                          * or the actual quota data files to be consistent
376                          * with how they are handled inside the kernel.
377                          */
378 #ifdef  SF_SNAPSHOT
379                         if (DIP(dp, di_flags) & SF_SNAPSHOT)
380                                 continue;
381 #endif
382                         if ((ino == userino && dev == userdev) ||
383                             (ino == groupino && dev == groupdev))
384                                 continue;
385                         if (qnp->flags & HASGRP) {
386                                 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
387                                     (char *)0, mntpt);
388                                 fup->fu_curinodes++;
389                                 if (mode == IFREG || mode == IFDIR ||
390                                     mode == IFLNK)
391                                         fup->fu_curblocks += DIP(dp, di_blocks);
392                         }
393                         if (qnp->flags & HASUSR) {
394                                 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
395                                     (char *)0, mntpt);
396                                 fup->fu_curinodes++;
397                                 if (mode == IFREG || mode == IFDIR ||
398                                     mode == IFLNK)
399                                         fup->fu_curblocks += DIP(dp, di_blocks);
400                         }
401                 }
402         }
403         freeinodebuf();
404         if (qnp->flags & HASUSR)
405                 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
406         if (qnp->flags & HASGRP)
407                 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
408         close(fi);
409         (void)fflush(stdout);
410         return (errs);
411 }
412
413 /*
414  * Update a specified quota file.
415  */
416 int
417 update(char *fsname, char *quotafile, int type)
418 {
419         struct fileusage *fup;
420         FILE *qfi, *qfo;
421         u_long id, lastid, highid = 0;
422         off_t offset;
423         int i;
424         struct dqblk dqbuf;
425         struct stat sb;
426         static int warned = 0;
427         static struct dqblk zerodqbuf;
428         static struct fileusage zerofileusage;
429
430         if ((qfo = fopen(quotafile, "r+")) == NULL) {
431                 if (errno == ENOENT)
432                         qfo = fopen(quotafile, "w+");
433                 if (qfo) {
434                         warnx("creating quota file %s", quotafile);
435 #define MODE    (S_IRUSR|S_IWUSR|S_IRGRP)
436                         (void) fchown(fileno(qfo), getuid(), getquotagid());
437                         (void) fchmod(fileno(qfo), MODE);
438                 } else {
439                         warn("%s", quotafile);
440                         return (1);
441                 }
442         }
443         if ((qfi = fopen(quotafile, "r")) == NULL) {
444                 warn("%s", quotafile);
445                 (void) fclose(qfo);
446                 return (1);
447         }
448         if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
449             errno == EOPNOTSUPP && !warned && vflag) {
450                 warned++;
451                 (void)printf("*** Warning: %s\n",
452                     "Quotas are not compiled into this kernel");
453         }
454         if (fstat(fileno(qfi), &sb) < 0) {
455                 warn("Cannot fstat quota file %s\n", quotafile);
456                 (void) fclose(qfo);
457                 (void) fclose(qfi);
458                 return (1);
459         }
460         if ((sb.st_size % sizeof(struct dqblk)) != 0)
461                 warn("%s size is not a multiple of dqblk\n", quotafile);
462
463         /*
464          * Scan the on-disk quota file and record any usage changes.
465          */
466
467         if (sb.st_size != 0)
468                 lastid = (sb.st_size / sizeof(struct dqblk)) - 1;
469         else
470                 lastid = 0;
471         for (id = 0, offset = 0; id <= lastid;
472             id++, offset += sizeof(struct dqblk)) {
473                 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
474                         dqbuf = zerodqbuf;
475                 if ((fup = lookup(id, type)) == NULL)
476                         fup = &zerofileusage;
477                 if (fup->fu_curinodes || fup->fu_curblocks ||
478                     dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
479                     dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
480                         highid = id;
481                 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
482                     dqbuf.dqb_curblocks == fup->fu_curblocks) {
483                         fup->fu_curinodes = 0;
484                         fup->fu_curblocks = 0;
485                         continue;
486                 }
487                 printchanges(fsname, type, &dqbuf, fup, id);
488                 /*
489                  * Reset time limit if have a soft limit and were
490                  * previously under it, but are now over it.
491                  */
492                 if (dqbuf.dqb_bsoftlimit && id != 0 &&
493                     dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
494                     fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
495                         dqbuf.dqb_btime = 0;
496                 if (dqbuf.dqb_isoftlimit && id != 0 &&
497                     dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
498                     fup->fu_curinodes >= dqbuf.dqb_isoftlimit)
499                         dqbuf.dqb_itime = 0;
500                 dqbuf.dqb_curinodes = fup->fu_curinodes;
501                 dqbuf.dqb_curblocks = fup->fu_curblocks;
502                 if (fseeko(qfo, offset, SEEK_SET) < 0) {
503                         warn("%s: seek failed", quotafile);
504                         return(1);
505                 }
506                 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
507                 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
508                     (caddr_t)&dqbuf);
509                 fup->fu_curinodes = 0;
510                 fup->fu_curblocks = 0;
511         }
512
513         /*
514          * Walk the hash table looking for ids with non-zero usage
515          * that are not currently recorded in the quota file. E.g.
516          * ids that are past the end of the current file.
517          */
518
519         for (i = 0; i < FUHASH; i++) {
520                 for (fup = fuhead[type][i]; fup != NULL; fup = fup->fu_next) {
521                         if (fup->fu_id <= lastid)
522                                 continue;
523                         if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
524                                 continue;
525                         bzero(&dqbuf, sizeof(struct dqblk));
526                         if (fup->fu_id > highid)
527                                 highid = fup->fu_id;
528                         printchanges(fsname, type, &dqbuf, fup, id);
529                         dqbuf.dqb_curinodes = fup->fu_curinodes;
530                         dqbuf.dqb_curblocks = fup->fu_curblocks;
531                         offset = (off_t)fup->fu_id * sizeof(struct dqblk);
532                         if (fseeko(qfo, offset, SEEK_SET) < 0) {
533                                 warn("%s: seek failed", quotafile);
534                                 return(1);
535                         }
536                         fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
537                         (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
538                             (caddr_t)&dqbuf);
539                         fup->fu_curinodes = 0;
540                         fup->fu_curblocks = 0;
541                 }
542         }
543         fclose(qfi);
544         fflush(qfo);
545         ftruncate(fileno(qfo),
546             (((off_t)highid + 1) * sizeof(struct dqblk)));
547         fclose(qfo);
548         return (0);
549 }
550
551 /*
552  * Check to see if target appears in list of size cnt.
553  */
554 int
555 oneof(char *target, char *list[], int cnt)
556 {
557         int i;
558
559         for (i = 0; i < cnt; i++)
560                 if (strcmp(target, list[i]) == 0)
561                         return (i);
562         return (-1);
563 }
564
565 /*
566  * Determine the group identifier for quota files.
567  */
568 int
569 getquotagid(void)
570 {
571         struct group *gr;
572
573         if ((gr = getgrnam(quotagroup)) != NULL)
574                 return (gr->gr_gid);
575         return (-1);
576 }
577
578 /*
579  * Check to see if a particular quota is to be enabled.
580  */
581 int
582 hasquota(struct fstab *fs, int type, char **qfnamep)
583 {
584         char *opt;
585         char *cp;
586         struct statfs sfb;
587         static char initname, usrname[100], grpname[100];
588         static char buf[BUFSIZ];
589
590         if (!initname) {
591                 (void)snprintf(usrname, sizeof(usrname), "%s%s",
592                     qfextension[USRQUOTA], qfname);
593                 (void)snprintf(grpname, sizeof(grpname), "%s%s",
594                     qfextension[GRPQUOTA], qfname);
595                 initname = 1;
596         }
597         strcpy(buf, fs->fs_mntops);
598         for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
599                 if ((cp = index(opt, '=')) != NULL)
600                         *cp++ = '\0';
601                 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
602                         break;
603                 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
604                         break;
605         }
606         if (!opt)
607                 return (0);
608         if (cp)
609                 *qfnamep = cp;
610         else {
611                 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
612                     qfname, qfextension[type]);
613                 *qfnamep = buf;
614         }
615         if (statfs(fs->fs_file, &sfb) != 0) {
616                 warn("cannot statfs mount point %s", fs->fs_file);
617                 return (0);
618         }
619         if (strcmp(fs->fs_file, sfb.f_mntonname)) {
620                 warnx("%s not mounted for %s quotas", fs->fs_file,
621                     type == USRQUOTA ? "user" : "group");
622                 return (0);
623         }
624         return (1);
625 }
626
627 /*
628  * Routines to manage the file usage table.
629  *
630  * Lookup an id of a specific type.
631  */
632 struct fileusage *
633 lookup(u_long id, int type)
634 {
635         struct fileusage *fup;
636
637         for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
638                 if (fup->fu_id == id)
639                         return (fup);
640         return (NULL);
641 }
642
643 /*
644  * Add a new file usage id if it does not already exist.
645  */
646 struct fileusage *
647 addid(u_long id, int type, char *name, char *fsname)
648 {
649         struct fileusage *fup, **fhp;
650         int len;
651
652         if ((fup = lookup(id, type)) != NULL)
653                 return (fup);
654         if (name)
655                 len = strlen(name);
656         else
657                 len = 0;
658         if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
659                 errx(1, "calloc failed");
660         fhp = &fuhead[type][id & (FUHASH - 1)];
661         fup->fu_next = *fhp;
662         *fhp = fup;
663         fup->fu_id = id;
664         if (name)
665                 bcopy(name, fup->fu_name, len + 1);
666         else {
667                 (void)sprintf(fup->fu_name, "%lu", id);
668                 if (vflag) {
669                         if (aflag && fsname != NULL)
670                                 (void)printf("%s: ", fsname);
671                         printf("unknown %cid: %lu\n",
672                             type == USRQUOTA ? 'u' : 'g', id);
673                 }
674         }
675         return (fup);
676 }
677
678 /*
679  * Special purpose version of ginode used to optimize pass
680  * over all the inodes in numerical order.
681  */
682 static ino_t nextino, lastinum, lastvalidinum;
683 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
684 static caddr_t inodebuf;
685 #define INOBUFSIZE      56*1024         /* size of buffer to read inodes */
686
687 union dinode *
688 getnextinode(ino_t inumber)
689 {
690         long size;
691         ufs2_daddr_t dblk;
692         union dinode *dp;
693         static caddr_t nextinop;
694
695         if (inumber != nextino++ || inumber > lastvalidinum)
696                 errx(1, "bad inode number %d to nextinode", inumber);
697         if (inumber >= lastinum) {
698                 readcnt++;
699                 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
700                 if (readcnt % readpercg == 0) {
701                         size = partialsize;
702                         lastinum += partialcnt;
703                 } else {
704                         size = inobufsize;
705                         lastinum += fullcnt;
706                 }
707                 /*
708                  * If bread returns an error, it will already have zeroed
709                  * out the buffer, so we do not need to do so here.
710                  */
711                 bread(dblk, inodebuf, size);
712                 nextinop = inodebuf;
713         }
714         dp = (union dinode *)nextinop;
715         if (sblock.fs_magic == FS_UFS1_MAGIC)
716                 nextinop += sizeof(struct ufs1_dinode);
717         else
718                 nextinop += sizeof(struct ufs2_dinode);
719         return (dp);
720 }
721
722 /*
723  * Prepare to scan a set of inodes.
724  */
725 void
726 setinodebuf(ino_t inum)
727 {
728
729         if (inum % sblock.fs_ipg != 0)
730                 errx(1, "bad inode number %d to setinodebuf", inum);
731         lastvalidinum = inum + sblock.fs_ipg - 1;
732         nextino = inum;
733         lastinum = inum;
734         readcnt = 0;
735         if (inodebuf != NULL)
736                 return;
737         inobufsize = blkroundup(&sblock, INOBUFSIZE);
738         fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
739             sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
740         readpercg = sblock.fs_ipg / fullcnt;
741         partialcnt = sblock.fs_ipg % fullcnt;
742         partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
743             sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
744         if (partialcnt != 0) {
745                 readpercg++;
746         } else {
747                 partialcnt = fullcnt;
748                 partialsize = inobufsize;
749         }
750         if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
751                 errx(1, "cannot allocate space for inode buffer");
752 }
753
754 /*
755  * Free up data structures used to scan inodes.
756  */
757 void
758 freeinodebuf(void)
759 {
760
761         if (inodebuf != NULL)
762                 free(inodebuf);
763         inodebuf = NULL;
764 }
765
766 /*
767  * Read specified disk blocks.
768  */
769 void
770 bread(ufs2_daddr_t bno, char *buf, long cnt)
771 {
772
773         if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
774             read(fi, buf, cnt) != cnt)
775                 errx(1, "bread failed on block %ld", (long)bno);
776 }
777
778 /*
779  * Display updated block and i-node counts.
780  */
781 void
782 printchanges(char *fsname, int type, struct dqblk *dp,
783     struct fileusage *fup, u_long id)
784 {
785         if (!vflag)
786                 return;
787         if (aflag)
788                 (void)printf("%s: ", fsname);
789         if (fup->fu_name[0] == '\0')
790                 (void)printf("%-8lu fixed ", id);
791         else
792                 (void)printf("%-8s fixed ", fup->fu_name);
793         switch (type) {
794
795         case GRPQUOTA:
796                 (void)printf("(group):");
797                 break;
798
799         case USRQUOTA:
800                 (void)printf("(user): ");
801                 break;
802
803         default:
804                 (void)printf("(unknown quota type %d)", type);
805                 break;
806         }
807         if (dp->dqb_curinodes != fup->fu_curinodes)
808                 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
809                     (u_long)fup->fu_curinodes);
810         if (dp->dqb_curblocks != fup->fu_curblocks)
811                 (void)printf("\tblocks %lu -> %lu",
812                     (u_long)dp->dqb_curblocks,
813                     (u_long)fup->fu_curblocks);
814         (void)printf("\n");
815 }