121519Smckusick /*
261869Sbostic * Copyright (c) 1980, 1990, 1993
361869Sbostic * The Regents of the University of California. All rights reserved.
434366Sbostic *
541424Smckusick * This code is derived from software contributed to Berkeley by
641424Smckusick * Robert Elz at The University of Melbourne.
741424Smckusick *
842817Sbostic * %sccs.include.redist.c%
921519Smckusick */
1021519Smckusick
1112718Smckusick #ifndef lint
1261869Sbostic static char copyright[] =
1361869Sbostic "@(#) Copyright (c) 1980, 1990, 1993\n\
1461869Sbostic The Regents of the University of California. All rights reserved.\n";
1534366Sbostic #endif /* not lint */
1612718Smckusick
1721519Smckusick #ifndef lint
18*67978Smckusick static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
1934366Sbostic #endif /* not lint */
2021519Smckusick
2112718Smckusick /*
2212718Smckusick * Quota report
2312718Smckusick */
2412718Smckusick #include <sys/param.h>
2512801Smckusick #include <sys/stat.h>
26*67978Smckusick #include <sys/queue.h>
2751633Sbostic #include <ufs/ufs/quota.h>
2812801Smckusick #include <fstab.h>
2912801Smckusick #include <pwd.h>
3041424Smckusick #include <grp.h>
3138519Sbostic #include <stdio.h>
3238519Sbostic #include <errno.h>
3312718Smckusick
3445254Smckusick char *qfname = QUOTAFILENAME;
3545254Smckusick char *qfextension[] = INITQFNAMES;
3645254Smckusick
3712801Smckusick struct fileusage {
3841424Smckusick struct fileusage *fu_next;
3941424Smckusick struct dqblk fu_dqblk;
4041424Smckusick u_long fu_id;
4141424Smckusick char fu_name[1];
4241424Smckusick /* actually bigger */
4312801Smckusick };
4441424Smckusick #define FUHASH 1024 /* must be power of two */
4541424Smckusick struct fileusage *fuhead[MAXQUOTAS][FUHASH];
4612801Smckusick struct fileusage *lookup();
4741424Smckusick struct fileusage *addid();
4841424Smckusick u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
4912718Smckusick
5041424Smckusick int vflag; /* verbose */
5141424Smckusick int aflag; /* all file systems */
5212718Smckusick
main(argc,argv)5312718Smckusick main(argc, argv)
5412801Smckusick int argc;
5512718Smckusick char **argv;
5612718Smckusick {
5712801Smckusick register struct fstab *fs;
5812801Smckusick register struct passwd *pw;
5941424Smckusick register struct group *gr;
6041424Smckusick int gflag = 0, uflag = 0, errs = 0;
6141424Smckusick long i, argnum, done = 0;
6241424Smckusick extern char *optarg;
6341424Smckusick extern int optind;
6441436Smckusick char ch, *qfnp;
6512718Smckusick
6641424Smckusick while ((ch = getopt(argc, argv, "aguv")) != EOF) {
6741424Smckusick switch(ch) {
6841424Smckusick case 'a':
6941424Smckusick aflag++;
7041424Smckusick break;
7141424Smckusick case 'g':
7241424Smckusick gflag++;
7341424Smckusick break;
7441424Smckusick case 'u':
7541424Smckusick uflag++;
7641424Smckusick break;
7741424Smckusick case 'v':
7841424Smckusick vflag++;
7941424Smckusick break;
8041424Smckusick default:
8141424Smckusick usage();
8241424Smckusick }
8312801Smckusick }
8441424Smckusick argc -= optind;
8541424Smckusick argv += optind;
8641424Smckusick if (argc == 0 && !aflag)
8741424Smckusick usage();
8841424Smckusick if (!gflag && !uflag) {
8941424Smckusick if (aflag)
9041424Smckusick gflag++;
9141424Smckusick uflag++;
9212801Smckusick }
9341424Smckusick if (gflag) {
9441424Smckusick setgrent();
9541424Smckusick while ((gr = getgrent()) != 0)
9641424Smckusick (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
9741424Smckusick endgrent();
9812718Smckusick }
9941424Smckusick if (uflag) {
10041424Smckusick setpwent();
10141424Smckusick while ((pw = getpwent()) != 0)
10241424Smckusick (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
10341424Smckusick endpwent();
10412801Smckusick }
10512801Smckusick setfsent();
10612801Smckusick while ((fs = getfsent()) != NULL) {
10741436Smckusick if (strcmp(fs->fs_vfstype, "ufs"))
10841436Smckusick continue;
10941424Smckusick if (aflag) {
11041436Smckusick if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
11141436Smckusick errs += repquota(fs, GRPQUOTA, qfnp);
11241436Smckusick if (uflag && hasquota(fs, USRQUOTA, &qfnp))
11341436Smckusick errs += repquota(fs, USRQUOTA, qfnp);
11412718Smckusick continue;
11541424Smckusick }
11641424Smckusick if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
11741424Smckusick (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
11841424Smckusick done |= 1 << argnum;
11941436Smckusick if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
12041436Smckusick errs += repquota(fs, GRPQUOTA, qfnp);
12141436Smckusick if (uflag && hasquota(fs, USRQUOTA, &qfnp))
12241436Smckusick errs += repquota(fs, USRQUOTA, qfnp);
12341424Smckusick }
12412718Smckusick }
12512801Smckusick endfsent();
12612801Smckusick for (i = 0; i < argc; i++)
12712801Smckusick if ((done & (1 << i)) == 0)
12841424Smckusick fprintf(stderr, "%s not found in fstab\n", argv[i]);
12912801Smckusick exit(errs);
13012801Smckusick }
13112718Smckusick
usage()13241424Smckusick usage()
13312801Smckusick {
13441424Smckusick fprintf(stderr, "Usage:\n\t%s\n\t%s\n",
13541424Smckusick "repquota [-v] [-g] [-u] -a",
13641424Smckusick "repquota [-v] [-g] [-u] filesys ...");
13741424Smckusick exit(1);
13841424Smckusick }
13941424Smckusick
repquota(fs,type,qfpathname)14041436Smckusick repquota(fs, type, qfpathname)
14141424Smckusick register struct fstab *fs;
14241424Smckusick int type;
14341436Smckusick char *qfpathname;
14441424Smckusick {
14512801Smckusick register struct fileusage *fup;
14612801Smckusick FILE *qf;
14741424Smckusick u_long id;
14812801Smckusick struct dqblk dqbuf;
14941436Smckusick char *timeprt();
15041424Smckusick static struct dqblk zerodqblk;
15121086Smckusick static int warned = 0;
15241424Smckusick static int multiple = 0;
15321086Smckusick extern int errno;
15412718Smckusick
15541424Smckusick if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
15641424Smckusick errno == EOPNOTSUPP && !warned && vflag) {
15721086Smckusick warned++;
15821086Smckusick fprintf(stdout,
15921086Smckusick "*** Warning: Quotas are not compiled into this kernel\n");
16021086Smckusick }
16141424Smckusick if (multiple++)
16241424Smckusick printf("\n");
16341424Smckusick if (vflag)
16441424Smckusick fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
16541424Smckusick qfextension[type], fs->fs_file, fs->fs_spec);
16641436Smckusick if ((qf = fopen(qfpathname, "r")) == NULL) {
16741436Smckusick perror(qfpathname);
16841424Smckusick return (1);
16941424Smckusick }
17041424Smckusick for (id = 0; ; id++) {
17112801Smckusick fread(&dqbuf, sizeof(struct dqblk), 1, qf);
17212801Smckusick if (feof(qf))
17312801Smckusick break;
17441424Smckusick if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
17512718Smckusick continue;
17641424Smckusick if ((fup = lookup(id, type)) == 0)
17741424Smckusick fup = addid(id, type, (char *)0);
17812801Smckusick fup->fu_dqblk = dqbuf;
17912801Smckusick }
18041424Smckusick fclose(qf);
18112801Smckusick printf(" Block limits File limits\n");
18241424Smckusick printf("User used soft hard grace used soft hard grace\n");
18341424Smckusick for (id = 0; id <= highid[type]; id++) {
18441424Smckusick fup = lookup(id, type);
18512801Smckusick if (fup == 0)
18612801Smckusick continue;
18726486Smckusick if (fup->fu_dqblk.dqb_curinodes == 0 &&
18812801Smckusick fup->fu_dqblk.dqb_curblocks == 0)
18912801Smckusick continue;
19041424Smckusick printf("%-10s", fup->fu_name);
19141424Smckusick printf("%c%c%8d%8d%8d%7s",
19212801Smckusick fup->fu_dqblk.dqb_bsoftlimit &&
19312801Smckusick fup->fu_dqblk.dqb_curblocks >=
19412801Smckusick fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
19512801Smckusick fup->fu_dqblk.dqb_isoftlimit &&
19612801Smckusick fup->fu_dqblk.dqb_curinodes >=
19712801Smckusick fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
19825401Sbloom dbtob(fup->fu_dqblk.dqb_curblocks) / 1024,
19925401Sbloom dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024,
20025401Sbloom dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024,
20141424Smckusick fup->fu_dqblk.dqb_bsoftlimit &&
20241424Smckusick fup->fu_dqblk.dqb_curblocks >=
20341424Smckusick fup->fu_dqblk.dqb_bsoftlimit ?
20441424Smckusick timeprt(fup->fu_dqblk.dqb_btime) : "");
20541424Smckusick printf(" %6d%6d%6d%7s\n",
20612801Smckusick fup->fu_dqblk.dqb_curinodes,
20712801Smckusick fup->fu_dqblk.dqb_isoftlimit,
20812801Smckusick fup->fu_dqblk.dqb_ihardlimit,
20941424Smckusick fup->fu_dqblk.dqb_isoftlimit &&
21041424Smckusick fup->fu_dqblk.dqb_curinodes >=
21141424Smckusick fup->fu_dqblk.dqb_isoftlimit ?
21241424Smckusick timeprt(fup->fu_dqblk.dqb_itime) : "");
21312801Smckusick fup->fu_dqblk = zerodqblk;
21412718Smckusick }
21512801Smckusick return (0);
21612718Smckusick }
21712718Smckusick
21841424Smckusick /*
21941424Smckusick * Check to see if target appears in list of size cnt.
22041424Smckusick */
oneof(target,list,cnt)22141424Smckusick oneof(target, list, cnt)
22241424Smckusick register char *target, *list[];
22341424Smckusick int cnt;
22412718Smckusick {
22512801Smckusick register int i;
22612718Smckusick
22741424Smckusick for (i = 0; i < cnt; i++)
22841424Smckusick if (strcmp(target, list[i]) == 0)
22941424Smckusick return (i);
23041424Smckusick return (-1);
23141424Smckusick }
23241424Smckusick
23341424Smckusick /*
23441424Smckusick * Check to see if a particular quota is to be enabled.
23541424Smckusick */
hasquota(fs,type,qfnamep)23641436Smckusick hasquota(fs, type, qfnamep)
23741436Smckusick register struct fstab *fs;
23841424Smckusick int type;
23941436Smckusick char **qfnamep;
24041424Smckusick {
24141424Smckusick register char *opt;
24241436Smckusick char *cp, *index(), *strtok();
24341424Smckusick static char initname, usrname[100], grpname[100];
24441436Smckusick static char buf[BUFSIZ];
24541424Smckusick
24641424Smckusick if (!initname) {
24741424Smckusick sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
24841424Smckusick sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
24941424Smckusick initname = 1;
25041424Smckusick }
25141436Smckusick strcpy(buf, fs->fs_mntops);
25241424Smckusick for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
25341436Smckusick if (cp = index(opt, '='))
25441436Smckusick *cp++ = '\0';
25541424Smckusick if (type == USRQUOTA && strcmp(opt, usrname) == 0)
25641436Smckusick break;
25741424Smckusick if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
25841436Smckusick break;
25941424Smckusick }
26041436Smckusick if (!opt)
26141436Smckusick return (0);
26241436Smckusick if (cp) {
26341436Smckusick *qfnamep = cp;
26441436Smckusick return (1);
26541436Smckusick }
26641436Smckusick (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
26741436Smckusick *qfnamep = buf;
26841436Smckusick return (1);
26912718Smckusick }
27012801Smckusick
27141424Smckusick /*
27241424Smckusick * Routines to manage the file usage table.
27341424Smckusick *
27441424Smckusick * Lookup an id of a specific type.
27541424Smckusick */
27612801Smckusick struct fileusage *
lookup(id,type)27741424Smckusick lookup(id, type)
27841424Smckusick u_long id;
27941424Smckusick int type;
28012801Smckusick {
28112801Smckusick register struct fileusage *fup;
28212801Smckusick
28341424Smckusick for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
28441424Smckusick if (fup->fu_id == id)
28512801Smckusick return (fup);
28612801Smckusick return ((struct fileusage *)0);
28712801Smckusick }
28812801Smckusick
28941424Smckusick /*
29041424Smckusick * Add a new file usage id if it does not already exist.
29141424Smckusick */
29212801Smckusick struct fileusage *
addid(id,type,name)29341424Smckusick addid(id, type, name)
29441424Smckusick u_long id;
29541424Smckusick int type;
29641424Smckusick char *name;
29712801Smckusick {
29812801Smckusick struct fileusage *fup, **fhp;
29941424Smckusick int len;
30024660Sserge extern char *calloc();
30112801Smckusick
30241424Smckusick if (fup = lookup(id, type))
30312801Smckusick return (fup);
30441424Smckusick if (name)
30541424Smckusick len = strlen(name);
30641424Smckusick else
30741424Smckusick len = 10;
30841424Smckusick if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) {
30912801Smckusick fprintf(stderr, "out of memory for fileusage structures\n");
31012801Smckusick exit(1);
31112801Smckusick }
31241424Smckusick fhp = &fuhead[type][id & (FUHASH - 1)];
31312801Smckusick fup->fu_next = *fhp;
31412801Smckusick *fhp = fup;
31541424Smckusick fup->fu_id = id;
31641424Smckusick if (id > highid[type])
31741424Smckusick highid[type] = id;
31841424Smckusick if (name) {
31941424Smckusick bcopy(name, fup->fu_name, len + 1);
32041424Smckusick } else {
32141424Smckusick sprintf(fup->fu_name, "%u", id);
32241424Smckusick }
32312801Smckusick return (fup);
32412801Smckusick }
32541424Smckusick
32641424Smckusick /*
32741424Smckusick * Calculate the grace period and return a printable string for it.
32841424Smckusick */
32941424Smckusick char *
timeprt(seconds)33041424Smckusick timeprt(seconds)
33141424Smckusick time_t seconds;
33241424Smckusick {
33341424Smckusick time_t hours, minutes;
33441424Smckusick static char buf[20];
33541424Smckusick static time_t now;
33641424Smckusick
33741424Smckusick if (now == 0)
33841424Smckusick time(&now);
33941424Smckusick if (now > seconds)
34041424Smckusick return ("none");
34141424Smckusick seconds -= now;
34241424Smckusick minutes = (seconds + 30) / 60;
34341424Smckusick hours = (minutes + 30) / 60;
34441424Smckusick if (hours >= 36) {
34541424Smckusick sprintf(buf, "%ddays", (hours + 12) / 24);
34641424Smckusick return (buf);
34741424Smckusick }
34841424Smckusick if (minutes >= 60) {
34941424Smckusick sprintf(buf, "%2d:%d", minutes / 60, minutes % 60);
35041424Smckusick return (buf);
35141424Smckusick }
35241424Smckusick sprintf(buf, "%2d", minutes);
35341424Smckusick return (buf);
35441424Smckusick }
355