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