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