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