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