quotacheck.c revision 1.1.1.1 1 1.1 cgd /*
2 1.1.1.1 mycroft * Copyright (c) 1980, 1990, 1993
3 1.1.1.1 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Robert Elz at The University of Melbourne.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1.1.1 mycroft static char copyright[] =
39 1.1.1.1 mycroft "@(#) Copyright (c) 1980, 1990, 1993\n\
40 1.1.1.1 mycroft The Regents of the University of California. All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.1.1.1 mycroft static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd /*
48 1.1 cgd * Fix up / report on disk quotas & usage
49 1.1 cgd */
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/stat.h>
52 1.1.1.1 mycroft
53 1.1.1.1 mycroft #include <ufs/ufs/dinode.h>
54 1.1.1.1 mycroft #include <ufs/ufs/quota.h>
55 1.1.1.1 mycroft #include <ufs/ffs/fs.h>
56 1.1.1.1 mycroft
57 1.1 cgd #include <fcntl.h>
58 1.1 cgd #include <fstab.h>
59 1.1 cgd #include <pwd.h>
60 1.1 cgd #include <grp.h>
61 1.1 cgd #include <errno.h>
62 1.1 cgd #include <unistd.h>
63 1.1 cgd #include <stdio.h>
64 1.1 cgd #include <stdlib.h>
65 1.1 cgd #include <string.h>
66 1.1 cgd
67 1.1 cgd char *qfname = QUOTAFILENAME;
68 1.1 cgd char *qfextension[] = INITQFNAMES;
69 1.1 cgd char *quotagroup = QUOTAGROUP;
70 1.1 cgd
71 1.1 cgd union {
72 1.1 cgd struct fs sblk;
73 1.1 cgd char dummy[MAXBSIZE];
74 1.1 cgd } un;
75 1.1 cgd #define sblock un.sblk
76 1.1 cgd long dev_bsize = 1;
77 1.1 cgd long maxino;
78 1.1 cgd
79 1.1 cgd struct quotaname {
80 1.1 cgd long flags;
81 1.1 cgd char grpqfname[MAXPATHLEN + 1];
82 1.1 cgd char usrqfname[MAXPATHLEN + 1];
83 1.1 cgd };
84 1.1 cgd #define HASUSR 1
85 1.1 cgd #define HASGRP 2
86 1.1 cgd
87 1.1 cgd struct fileusage {
88 1.1 cgd struct fileusage *fu_next;
89 1.1 cgd u_long fu_curinodes;
90 1.1 cgd u_long fu_curblocks;
91 1.1 cgd u_long fu_id;
92 1.1 cgd char fu_name[1];
93 1.1 cgd /* actually bigger */
94 1.1 cgd };
95 1.1 cgd #define FUHASH 1024 /* must be power of two */
96 1.1 cgd struct fileusage *fuhead[MAXQUOTAS][FUHASH];
97 1.1 cgd
98 1.1 cgd int aflag; /* all file systems */
99 1.1 cgd int gflag; /* check group quotas */
100 1.1 cgd int uflag; /* check user quotas */
101 1.1 cgd int vflag; /* verbose */
102 1.1 cgd int fi; /* open disk file descriptor */
103 1.1 cgd u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
104 1.1 cgd
105 1.1.1.1 mycroft struct fileusage *
106 1.1.1.1 mycroft addid __P((u_long, int, char *));
107 1.1.1.1 mycroft char *blockcheck __P((char *));
108 1.1.1.1 mycroft void bread __P((daddr_t, char *, long));
109 1.1.1.1 mycroft int chkquota __P((char *, char *, struct quotaname *));
110 1.1.1.1 mycroft void err __P((const char *, ...));
111 1.1.1.1 mycroft void freeinodebuf __P((void));
112 1.1.1.1 mycroft struct dinode *
113 1.1.1.1 mycroft getnextinode __P((ino_t));
114 1.1.1.1 mycroft int getquotagid __P((void));
115 1.1.1.1 mycroft int hasquota __P((struct fstab *, int, char **));
116 1.1.1.1 mycroft struct fileusage *
117 1.1.1.1 mycroft lookup __P((u_long, int));
118 1.1.1.1 mycroft void *needchk __P((struct fstab *));
119 1.1.1.1 mycroft int oneof __P((char *, char*[], int));
120 1.1.1.1 mycroft void resetinodebuf __P((void));
121 1.1.1.1 mycroft int update __P((char *, char *, int));
122 1.1.1.1 mycroft void usage __P((void));
123 1.1.1.1 mycroft
124 1.1.1.1 mycroft int
125 1.1 cgd main(argc, argv)
126 1.1 cgd int argc;
127 1.1.1.1 mycroft char *argv[];
128 1.1 cgd {
129 1.1 cgd register struct fstab *fs;
130 1.1 cgd register struct passwd *pw;
131 1.1 cgd register struct group *gr;
132 1.1.1.1 mycroft struct quotaname *auxdata;
133 1.1.1.1 mycroft int i, argnum, maxrun, errs;
134 1.1.1.1 mycroft long done = 0;
135 1.1.1.1 mycroft char ch, *name;
136 1.1 cgd
137 1.1.1.1 mycroft errs = maxrun = 0;
138 1.1 cgd while ((ch = getopt(argc, argv, "aguvl:")) != EOF) {
139 1.1 cgd switch(ch) {
140 1.1 cgd case 'a':
141 1.1 cgd aflag++;
142 1.1 cgd break;
143 1.1 cgd case 'g':
144 1.1 cgd gflag++;
145 1.1 cgd break;
146 1.1 cgd case 'u':
147 1.1 cgd uflag++;
148 1.1 cgd break;
149 1.1 cgd case 'v':
150 1.1 cgd vflag++;
151 1.1 cgd break;
152 1.1 cgd case 'l':
153 1.1 cgd maxrun = atoi(optarg);
154 1.1 cgd break;
155 1.1 cgd default:
156 1.1 cgd usage();
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd argc -= optind;
160 1.1 cgd argv += optind;
161 1.1 cgd if ((argc == 0 && !aflag) || (argc > 0 && aflag))
162 1.1 cgd usage();
163 1.1 cgd if (!gflag && !uflag) {
164 1.1 cgd gflag++;
165 1.1 cgd uflag++;
166 1.1 cgd }
167 1.1 cgd if (gflag) {
168 1.1 cgd setgrent();
169 1.1 cgd while ((gr = getgrent()) != 0)
170 1.1 cgd (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
171 1.1 cgd endgrent();
172 1.1 cgd }
173 1.1 cgd if (uflag) {
174 1.1 cgd setpwent();
175 1.1 cgd while ((pw = getpwent()) != 0)
176 1.1 cgd (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
177 1.1 cgd endpwent();
178 1.1 cgd }
179 1.1 cgd if (aflag)
180 1.1 cgd exit(checkfstab(1, maxrun, needchk, chkquota));
181 1.1.1.1 mycroft if (setfsent() == 0)
182 1.1.1.1 mycroft err("%s: can't open", FSTAB);
183 1.1 cgd while ((fs = getfsent()) != NULL) {
184 1.1 cgd if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
185 1.1 cgd (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
186 1.1 cgd (auxdata = needchk(fs)) &&
187 1.1 cgd (name = blockcheck(fs->fs_spec))) {
188 1.1 cgd done |= 1 << argnum;
189 1.1 cgd errs += chkquota(name, fs->fs_file, auxdata);
190 1.1 cgd }
191 1.1 cgd }
192 1.1 cgd endfsent();
193 1.1 cgd for (i = 0; i < argc; i++)
194 1.1 cgd if ((done & (1 << i)) == 0)
195 1.1 cgd fprintf(stderr, "%s not found in %s\n",
196 1.1 cgd argv[i], FSTAB);
197 1.1 cgd exit(errs);
198 1.1 cgd }
199 1.1 cgd
200 1.1.1.1 mycroft void
201 1.1 cgd usage()
202 1.1 cgd {
203 1.1.1.1 mycroft (void)fprintf(stderr, "usage:\t%s\n\t%s\n",
204 1.1 cgd "quotacheck -a [-guv]",
205 1.1 cgd "quotacheck [-guv] filesys ...");
206 1.1 cgd exit(1);
207 1.1 cgd }
208 1.1 cgd
209 1.1.1.1 mycroft void *
210 1.1 cgd needchk(fs)
211 1.1 cgd register struct fstab *fs;
212 1.1 cgd {
213 1.1 cgd register struct quotaname *qnp;
214 1.1 cgd char *qfnp;
215 1.1 cgd
216 1.1 cgd if (strcmp(fs->fs_vfstype, "ufs") ||
217 1.1 cgd strcmp(fs->fs_type, FSTAB_RW))
218 1.1.1.1 mycroft return (NULL);
219 1.1.1.1 mycroft if ((qnp = malloc(sizeof(*qnp))) == NULL)
220 1.1.1.1 mycroft err("%s", strerror(errno));
221 1.1 cgd qnp->flags = 0;
222 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
223 1.1 cgd strcpy(qnp->grpqfname, qfnp);
224 1.1 cgd qnp->flags |= HASGRP;
225 1.1 cgd }
226 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
227 1.1 cgd strcpy(qnp->usrqfname, qfnp);
228 1.1 cgd qnp->flags |= HASUSR;
229 1.1 cgd }
230 1.1 cgd if (qnp->flags)
231 1.1.1.1 mycroft return (qnp);
232 1.1.1.1 mycroft free(qnp);
233 1.1.1.1 mycroft return (NULL);
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd /*
237 1.1 cgd * Scan the specified filesystem to check quota(s) present on it.
238 1.1 cgd */
239 1.1.1.1 mycroft int
240 1.1 cgd chkquota(fsname, mntpt, qnp)
241 1.1 cgd char *fsname, *mntpt;
242 1.1 cgd register struct quotaname *qnp;
243 1.1 cgd {
244 1.1 cgd register struct fileusage *fup;
245 1.1 cgd register struct dinode *dp;
246 1.1 cgd int cg, i, mode, errs = 0;
247 1.1 cgd ino_t ino;
248 1.1 cgd
249 1.1.1.1 mycroft if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
250 1.1 cgd perror(fsname);
251 1.1 cgd return (1);
252 1.1 cgd }
253 1.1 cgd if (vflag) {
254 1.1.1.1 mycroft (void)printf("*** Checking ");
255 1.1 cgd if (qnp->flags & HASUSR)
256 1.1.1.1 mycroft (void)printf("%s%s", qfextension[USRQUOTA],
257 1.1 cgd (qnp->flags & HASGRP) ? " and " : "");
258 1.1 cgd if (qnp->flags & HASGRP)
259 1.1.1.1 mycroft (void)printf("%s", qfextension[GRPQUOTA]);
260 1.1.1.1 mycroft (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
261 1.1 cgd }
262 1.1 cgd sync();
263 1.1 cgd bread(SBOFF, (char *)&sblock, (long)SBSIZE);
264 1.1 cgd dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
265 1.1 cgd maxino = sblock.fs_ncg * sblock.fs_ipg;
266 1.1 cgd resetinodebuf();
267 1.1 cgd for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
268 1.1 cgd for (i = 0; i < sblock.fs_ipg; i++, ino++) {
269 1.1 cgd if (ino < ROOTINO)
270 1.1 cgd continue;
271 1.1 cgd if ((dp = getnextinode(ino)) == NULL)
272 1.1 cgd continue;
273 1.1 cgd if ((mode = dp->di_mode & IFMT) == 0)
274 1.1 cgd continue;
275 1.1 cgd if (qnp->flags & HASGRP) {
276 1.1 cgd fup = addid((u_long)dp->di_gid, GRPQUOTA,
277 1.1 cgd (char *)0);
278 1.1 cgd fup->fu_curinodes++;
279 1.1 cgd if (mode == IFREG || mode == IFDIR ||
280 1.1 cgd mode == IFLNK)
281 1.1 cgd fup->fu_curblocks += dp->di_blocks;
282 1.1 cgd }
283 1.1 cgd if (qnp->flags & HASUSR) {
284 1.1 cgd fup = addid((u_long)dp->di_uid, USRQUOTA,
285 1.1 cgd (char *)0);
286 1.1 cgd fup->fu_curinodes++;
287 1.1 cgd if (mode == IFREG || mode == IFDIR ||
288 1.1 cgd mode == IFLNK)
289 1.1 cgd fup->fu_curblocks += dp->di_blocks;
290 1.1 cgd }
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd freeinodebuf();
294 1.1 cgd if (qnp->flags & HASUSR)
295 1.1 cgd errs += update(mntpt, qnp->usrqfname, USRQUOTA);
296 1.1 cgd if (qnp->flags & HASGRP)
297 1.1 cgd errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
298 1.1 cgd close(fi);
299 1.1 cgd return (errs);
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * Update a specified quota file.
304 1.1 cgd */
305 1.1.1.1 mycroft int
306 1.1 cgd update(fsname, quotafile, type)
307 1.1 cgd char *fsname, *quotafile;
308 1.1 cgd register int type;
309 1.1 cgd {
310 1.1 cgd register struct fileusage *fup;
311 1.1 cgd register FILE *qfi, *qfo;
312 1.1 cgd register u_long id, lastid;
313 1.1 cgd struct dqblk dqbuf;
314 1.1 cgd static int warned = 0;
315 1.1 cgd static struct dqblk zerodqbuf;
316 1.1 cgd static struct fileusage zerofileusage;
317 1.1 cgd
318 1.1 cgd if ((qfo = fopen(quotafile, "r+")) == NULL) {
319 1.1 cgd if (errno == ENOENT)
320 1.1 cgd qfo = fopen(quotafile, "w+");
321 1.1 cgd if (qfo) {
322 1.1 cgd (void) fprintf(stderr,
323 1.1 cgd "quotacheck: creating quota file %s\n", quotafile);
324 1.1 cgd #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
325 1.1 cgd (void) fchown(fileno(qfo), getuid(), getquotagid());
326 1.1 cgd (void) fchmod(fileno(qfo), MODE);
327 1.1 cgd } else {
328 1.1 cgd (void) fprintf(stderr,
329 1.1 cgd "quotacheck: %s: %s\n", quotafile, strerror(errno));
330 1.1 cgd return (1);
331 1.1 cgd }
332 1.1 cgd }
333 1.1 cgd if ((qfi = fopen(quotafile, "r")) == NULL) {
334 1.1 cgd (void) fprintf(stderr,
335 1.1 cgd "quotacheck: %s: %s\n", quotafile, strerror(errno));
336 1.1 cgd (void) fclose(qfo);
337 1.1 cgd return (1);
338 1.1 cgd }
339 1.1 cgd if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
340 1.1 cgd errno == EOPNOTSUPP && !warned && vflag) {
341 1.1 cgd warned++;
342 1.1.1.1 mycroft (void)printf("*** Warning: %s\n",
343 1.1 cgd "Quotas are not compiled into this kernel");
344 1.1 cgd }
345 1.1 cgd for (lastid = highid[type], id = 0; id <= lastid; id++) {
346 1.1 cgd if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
347 1.1 cgd dqbuf = zerodqbuf;
348 1.1 cgd if ((fup = lookup(id, type)) == 0)
349 1.1 cgd fup = &zerofileusage;
350 1.1 cgd if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
351 1.1 cgd dqbuf.dqb_curblocks == fup->fu_curblocks) {
352 1.1 cgd fup->fu_curinodes = 0;
353 1.1 cgd fup->fu_curblocks = 0;
354 1.1 cgd fseek(qfo, (long)sizeof(struct dqblk), 1);
355 1.1 cgd continue;
356 1.1 cgd }
357 1.1 cgd if (vflag) {
358 1.1 cgd if (aflag)
359 1.1 cgd printf("%s: ", fsname);
360 1.1 cgd printf("%-8s fixed:", fup->fu_name);
361 1.1 cgd if (dqbuf.dqb_curinodes != fup->fu_curinodes)
362 1.1.1.1 mycroft (void)printf("\tinodes %d -> %d",
363 1.1 cgd dqbuf.dqb_curinodes, fup->fu_curinodes);
364 1.1 cgd if (dqbuf.dqb_curblocks != fup->fu_curblocks)
365 1.1.1.1 mycroft (void)printf("\tblocks %d -> %d",
366 1.1 cgd dqbuf.dqb_curblocks, fup->fu_curblocks);
367 1.1.1.1 mycroft (void)printf("\n");
368 1.1 cgd }
369 1.1 cgd /*
370 1.1 cgd * Reset time limit if have a soft limit and were
371 1.1 cgd * previously under it, but are now over it.
372 1.1 cgd */
373 1.1 cgd if (dqbuf.dqb_bsoftlimit &&
374 1.1 cgd dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
375 1.1 cgd fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
376 1.1 cgd dqbuf.dqb_btime = 0;
377 1.1 cgd if (dqbuf.dqb_isoftlimit &&
378 1.1 cgd dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
379 1.1 cgd fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
380 1.1 cgd dqbuf.dqb_itime = 0;
381 1.1 cgd dqbuf.dqb_curinodes = fup->fu_curinodes;
382 1.1 cgd dqbuf.dqb_curblocks = fup->fu_curblocks;
383 1.1 cgd fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
384 1.1 cgd (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
385 1.1 cgd (caddr_t)&dqbuf);
386 1.1 cgd fup->fu_curinodes = 0;
387 1.1 cgd fup->fu_curblocks = 0;
388 1.1 cgd }
389 1.1 cgd fclose(qfi);
390 1.1 cgd fflush(qfo);
391 1.1 cgd ftruncate(fileno(qfo),
392 1.1 cgd (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
393 1.1 cgd fclose(qfo);
394 1.1 cgd return (0);
395 1.1 cgd }
396 1.1 cgd
397 1.1 cgd /*
398 1.1 cgd * Check to see if target appears in list of size cnt.
399 1.1 cgd */
400 1.1.1.1 mycroft int
401 1.1 cgd oneof(target, list, cnt)
402 1.1 cgd register char *target, *list[];
403 1.1 cgd int cnt;
404 1.1 cgd {
405 1.1 cgd register int i;
406 1.1 cgd
407 1.1 cgd for (i = 0; i < cnt; i++)
408 1.1 cgd if (strcmp(target, list[i]) == 0)
409 1.1 cgd return (i);
410 1.1 cgd return (-1);
411 1.1 cgd }
412 1.1 cgd
413 1.1 cgd /*
414 1.1 cgd * Determine the group identifier for quota files.
415 1.1 cgd */
416 1.1.1.1 mycroft int
417 1.1 cgd getquotagid()
418 1.1 cgd {
419 1.1 cgd struct group *gr;
420 1.1 cgd
421 1.1 cgd if (gr = getgrnam(quotagroup))
422 1.1 cgd return (gr->gr_gid);
423 1.1 cgd return (-1);
424 1.1 cgd }
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * Check to see if a particular quota is to be enabled.
428 1.1 cgd */
429 1.1.1.1 mycroft int
430 1.1 cgd hasquota(fs, type, qfnamep)
431 1.1 cgd register struct fstab *fs;
432 1.1 cgd int type;
433 1.1 cgd char **qfnamep;
434 1.1 cgd {
435 1.1 cgd register char *opt;
436 1.1.1.1 mycroft char *cp;
437 1.1 cgd static char initname, usrname[100], grpname[100];
438 1.1 cgd static char buf[BUFSIZ];
439 1.1 cgd
440 1.1 cgd if (!initname) {
441 1.1.1.1 mycroft (void)snprintf(usrname, sizeof(usrname),
442 1.1.1.1 mycroft "%s%s", qfextension[USRQUOTA], qfname);
443 1.1.1.1 mycroft (void)snprintf(grpname, sizeof(grpname),
444 1.1.1.1 mycroft "%s%s", qfextension[GRPQUOTA], qfname);
445 1.1 cgd initname = 1;
446 1.1 cgd }
447 1.1 cgd strcpy(buf, fs->fs_mntops);
448 1.1 cgd for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
449 1.1 cgd if (cp = index(opt, '='))
450 1.1 cgd *cp++ = '\0';
451 1.1 cgd if (type == USRQUOTA && strcmp(opt, usrname) == 0)
452 1.1 cgd break;
453 1.1 cgd if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
454 1.1 cgd break;
455 1.1 cgd }
456 1.1 cgd if (!opt)
457 1.1 cgd return (0);
458 1.1.1.1 mycroft if (cp)
459 1.1 cgd *qfnamep = cp;
460 1.1.1.1 mycroft else {
461 1.1.1.1 mycroft (void)snprintf(buf, sizeof(buf),
462 1.1.1.1 mycroft "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
463 1.1.1.1 mycroft *qfnamep = buf;
464 1.1 cgd }
465 1.1 cgd return (1);
466 1.1 cgd }
467 1.1 cgd
468 1.1 cgd /*
469 1.1 cgd * Routines to manage the file usage table.
470 1.1 cgd *
471 1.1 cgd * Lookup an id of a specific type.
472 1.1 cgd */
473 1.1 cgd struct fileusage *
474 1.1 cgd lookup(id, type)
475 1.1 cgd u_long id;
476 1.1 cgd int type;
477 1.1 cgd {
478 1.1 cgd register struct fileusage *fup;
479 1.1 cgd
480 1.1 cgd for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
481 1.1 cgd if (fup->fu_id == id)
482 1.1 cgd return (fup);
483 1.1.1.1 mycroft return (NULL);
484 1.1 cgd }
485 1.1 cgd
486 1.1 cgd /*
487 1.1 cgd * Add a new file usage id if it does not already exist.
488 1.1 cgd */
489 1.1 cgd struct fileusage *
490 1.1 cgd addid(id, type, name)
491 1.1 cgd u_long id;
492 1.1 cgd int type;
493 1.1 cgd char *name;
494 1.1 cgd {
495 1.1 cgd struct fileusage *fup, **fhp;
496 1.1 cgd int len;
497 1.1 cgd
498 1.1 cgd if (fup = lookup(id, type))
499 1.1 cgd return (fup);
500 1.1 cgd if (name)
501 1.1 cgd len = strlen(name);
502 1.1 cgd else
503 1.1 cgd len = 10;
504 1.1.1.1 mycroft if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
505 1.1.1.1 mycroft err("%s", strerror(errno));
506 1.1 cgd fhp = &fuhead[type][id & (FUHASH - 1)];
507 1.1 cgd fup->fu_next = *fhp;
508 1.1 cgd *fhp = fup;
509 1.1 cgd fup->fu_id = id;
510 1.1 cgd if (id > highid[type])
511 1.1 cgd highid[type] = id;
512 1.1.1.1 mycroft if (name)
513 1.1 cgd bcopy(name, fup->fu_name, len + 1);
514 1.1.1.1 mycroft else
515 1.1.1.1 mycroft (void)sprintf(fup->fu_name, "%u", id);
516 1.1 cgd return (fup);
517 1.1 cgd }
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Special purpose version of ginode used to optimize pass
521 1.1 cgd * over all the inodes in numerical order.
522 1.1 cgd */
523 1.1 cgd ino_t nextino, lastinum;
524 1.1 cgd long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
525 1.1 cgd struct dinode *inodebuf;
526 1.1 cgd #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
527 1.1 cgd
528 1.1 cgd struct dinode *
529 1.1 cgd getnextinode(inumber)
530 1.1 cgd ino_t inumber;
531 1.1 cgd {
532 1.1 cgd long size;
533 1.1 cgd daddr_t dblk;
534 1.1 cgd static struct dinode *dp;
535 1.1 cgd
536 1.1.1.1 mycroft if (inumber != nextino++ || inumber > maxino)
537 1.1.1.1 mycroft err("bad inode number %d to nextinode", inumber);
538 1.1 cgd if (inumber >= lastinum) {
539 1.1 cgd readcnt++;
540 1.1.1.1 mycroft dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
541 1.1 cgd if (readcnt % readpercg == 0) {
542 1.1 cgd size = partialsize;
543 1.1 cgd lastinum += partialcnt;
544 1.1 cgd } else {
545 1.1 cgd size = inobufsize;
546 1.1 cgd lastinum += fullcnt;
547 1.1 cgd }
548 1.1 cgd bread(dblk, (char *)inodebuf, size);
549 1.1 cgd dp = inodebuf;
550 1.1 cgd }
551 1.1 cgd return (dp++);
552 1.1 cgd }
553 1.1 cgd
554 1.1 cgd /*
555 1.1 cgd * Prepare to scan a set of inodes.
556 1.1 cgd */
557 1.1.1.1 mycroft void
558 1.1 cgd resetinodebuf()
559 1.1 cgd {
560 1.1 cgd
561 1.1 cgd nextino = 0;
562 1.1 cgd lastinum = 0;
563 1.1 cgd readcnt = 0;
564 1.1 cgd inobufsize = blkroundup(&sblock, INOBUFSIZE);
565 1.1 cgd fullcnt = inobufsize / sizeof(struct dinode);
566 1.1 cgd readpercg = sblock.fs_ipg / fullcnt;
567 1.1 cgd partialcnt = sblock.fs_ipg % fullcnt;
568 1.1 cgd partialsize = partialcnt * sizeof(struct dinode);
569 1.1 cgd if (partialcnt != 0) {
570 1.1 cgd readpercg++;
571 1.1 cgd } else {
572 1.1 cgd partialcnt = fullcnt;
573 1.1 cgd partialsize = inobufsize;
574 1.1 cgd }
575 1.1 cgd if (inodebuf == NULL &&
576 1.1.1.1 mycroft (inodebuf = malloc((u_int)inobufsize)) == NULL)
577 1.1.1.1 mycroft err("%s", strerror(errno));
578 1.1 cgd while (nextino < ROOTINO)
579 1.1 cgd getnextinode(nextino);
580 1.1 cgd }
581 1.1 cgd
582 1.1 cgd /*
583 1.1 cgd * Free up data structures used to scan inodes.
584 1.1 cgd */
585 1.1.1.1 mycroft void
586 1.1 cgd freeinodebuf()
587 1.1 cgd {
588 1.1 cgd
589 1.1 cgd if (inodebuf != NULL)
590 1.1.1.1 mycroft free(inodebuf);
591 1.1 cgd inodebuf = NULL;
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd /*
595 1.1 cgd * Read specified disk blocks.
596 1.1 cgd */
597 1.1.1.1 mycroft void
598 1.1 cgd bread(bno, buf, cnt)
599 1.1 cgd daddr_t bno;
600 1.1 cgd char *buf;
601 1.1 cgd long cnt;
602 1.1 cgd {
603 1.1 cgd
604 1.1.1.1 mycroft if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
605 1.1.1.1 mycroft read(fi, buf, cnt) != cnt)
606 1.1.1.1 mycroft err("block %ld", bno);
607 1.1.1.1 mycroft }
608 1.1.1.1 mycroft
609 1.1.1.1 mycroft #if __STDC__
610 1.1.1.1 mycroft #include <stdarg.h>
611 1.1.1.1 mycroft #else
612 1.1.1.1 mycroft #include <varargs.h>
613 1.1.1.1 mycroft #endif
614 1.1.1.1 mycroft
615 1.1.1.1 mycroft void
616 1.1.1.1 mycroft #if __STDC__
617 1.1.1.1 mycroft err(const char *fmt, ...)
618 1.1.1.1 mycroft #else
619 1.1.1.1 mycroft err(fmt, va_alist)
620 1.1.1.1 mycroft char *fmt;
621 1.1.1.1 mycroft va_dcl
622 1.1.1.1 mycroft #endif
623 1.1.1.1 mycroft {
624 1.1.1.1 mycroft va_list ap;
625 1.1.1.1 mycroft #if __STDC__
626 1.1.1.1 mycroft va_start(ap, fmt);
627 1.1.1.1 mycroft #else
628 1.1.1.1 mycroft va_start(ap);
629 1.1.1.1 mycroft #endif
630 1.1.1.1 mycroft (void)fprintf(stderr, "quotacheck: ");
631 1.1.1.1 mycroft (void)vfprintf(stderr, fmt, ap);
632 1.1.1.1 mycroft va_end(ap);
633 1.1.1.1 mycroft (void)fprintf(stderr, "\n");
634 1.1.1.1 mycroft exit(1);
635 1.1.1.1 mycroft /* NOTREACHED */
636 1.1 cgd }
637