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