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