11553Srgrimes/*
21553Srgrimes * Copyright (c) 1980, 1990, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * This code is derived from software contributed to Berkeley by
61553Srgrimes * Robert Elz at The University of Melbourne.
71553Srgrimes *
81553Srgrimes * Redistribution and use in source and binary forms, with or without
91553Srgrimes * modification, are permitted provided that the following conditions
101553Srgrimes * are met:
111553Srgrimes * 1. Redistributions of source code must retain the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer.
131553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer in the
151553Srgrimes *    documentation and/or other materials provided with the distribution.
161553Srgrimes * 4. Neither the name of the University nor the names of its contributors
171553Srgrimes *    may be used to endorse or promote products derived from this software
181553Srgrimes *    without specific prior written permission.
191553Srgrimes *
201553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301553Srgrimes * SUCH DAMAGE.
311553Srgrimes */
321553Srgrimes
33114601Sobrien#if 0
341553Srgrimes#ifndef lint
3530373Scharnierstatic const char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1980, 1990, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
411553Srgrimesstatic char sccsid[] = "@(#)repquota.c	8.1 (Berkeley) 6/6/93";
42114601Sobrien#endif /* not lint */
4330373Scharnier#endif
44114601Sobrien#include <sys/cdefs.h>
45114601Sobrien__FBSDID("$FreeBSD$");
461553Srgrimes
471553Srgrimes/*
481553Srgrimes * Quota report
491553Srgrimes */
501553Srgrimes#include <sys/param.h>
51166485Smpp#include <sys/mount.h>
52207736Smckusick
531553Srgrimes#include <ufs/ufs/quota.h>
54207736Smckusick
5530373Scharnier#include <err.h>
5630373Scharnier#include <errno.h>
57207736Smckusick#include <fcntl.h>
581553Srgrimes#include <fstab.h>
5930373Scharnier#include <grp.h>
60207736Smckusick#include <libutil.h>
611553Srgrimes#include <pwd.h>
62207736Smckusick#include <stdint.h>
631553Srgrimes#include <stdio.h>
6430373Scharnier#include <stdlib.h>
6530373Scharnier#include <string.h>
6691233Sbde#include <time.h>
6730373Scharnier#include <unistd.h>
681553Srgrimes
6952734Sjulian/* Let's be paranoid about block size */
7052734Sjulian#if 10 > DEV_BSHIFT
7152734Sjulian#define dbtokb(db) \
7252734Sjulian	((off_t)(db) >> (10-DEV_BSHIFT))
7352734Sjulian#elif 10 < DEV_BSHIFT
7452734Sjulian#define dbtokb(db) \
7552734Sjulian	((off_t)(db) << (DEV_BSHIFT-10))
7652734Sjulian#else
7752734Sjulian#define dbtokb(db)	(db)
7852734Sjulian#endif
7952734Sjulian
8036993Sache#define max(a,b) ((a) >= (b) ? (a) : (b))
8136993Sache
82241737Sedstatic const char *qfextension[] = INITQFNAMES;
831553Srgrimes
841553Srgrimesstruct fileusage {
851553Srgrimes	struct	fileusage *fu_next;
861553Srgrimes	u_long	fu_id;
871553Srgrimes	char	fu_name[1];
881553Srgrimes	/* actually bigger */
891553Srgrimes};
901553Srgrimes#define FUHASH 1024	/* must be power of two */
91241737Sedstatic struct fileusage *fuhead[MAXQUOTAS][FUHASH];
92241737Sedstatic struct fileusage *lookup(u_long, int);
93241737Sedstatic struct fileusage *addid(u_long, int, char *);
94241737Sedstatic u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
951553Srgrimes
96241737Sedstatic int	vflag;		/* verbose */
97241737Sedstatic int	aflag;		/* all filesystems */
98241737Sedstatic int	nflag;		/* display user/group by id */
99241737Sedstatic int	hflag;		/* display in human readable format */
1001553Srgrimes
10199822Salfredint oneof(char *, char *[], int);
102207736Smckusickint repquota(struct fstab *, int);
10399822Salfredchar *timeprt(time_t);
104207736Smckusickstatic void prthumanval(int64_t bytes);
10599822Salfredstatic void usage(void);
10630373Scharnier
10730373Scharnierint
108180187Sdesmain(int argc, char *argv[])
1091553Srgrimes{
110180187Sdes	struct fstab *fs;
111180187Sdes	struct passwd *pw;
112180187Sdes	struct group *gr;
113124830Sgrehan	int ch, gflag = 0, uflag = 0, errs = 0;
1141553Srgrimes	long i, argnum, done = 0;
1151553Srgrimes
116207736Smckusick	while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
1171553Srgrimes		switch(ch) {
1181553Srgrimes		case 'a':
1191553Srgrimes			aflag++;
1201553Srgrimes			break;
1211553Srgrimes		case 'g':
1221553Srgrimes			gflag++;
1231553Srgrimes			break;
124207736Smckusick		case 'h':
125207736Smckusick			hflag++;
126207736Smckusick			break;
127117318Sbrooks		case 'n':
128117318Sbrooks			nflag++;
129117318Sbrooks			break;
1301553Srgrimes		case 'u':
1311553Srgrimes			uflag++;
1321553Srgrimes			break;
1331553Srgrimes		case 'v':
1341553Srgrimes			vflag++;
1351553Srgrimes			break;
1361553Srgrimes		default:
1371553Srgrimes			usage();
1381553Srgrimes		}
1391553Srgrimes	}
1401553Srgrimes	argc -= optind;
1411553Srgrimes	argv += optind;
1421553Srgrimes	if (argc == 0 && !aflag)
1431553Srgrimes		usage();
1441553Srgrimes	if (!gflag && !uflag) {
1451553Srgrimes		if (aflag)
1461553Srgrimes			gflag++;
1471553Srgrimes		uflag++;
1481553Srgrimes	}
149117318Sbrooks	if (gflag && !nflag) {
1501553Srgrimes		setgrent();
1511553Srgrimes		while ((gr = getgrent()) != 0)
1521553Srgrimes			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
1531553Srgrimes		endgrent();
1541553Srgrimes	}
155117318Sbrooks	if (uflag && !nflag) {
1561553Srgrimes		setpwent();
1571553Srgrimes		while ((pw = getpwent()) != 0)
1581553Srgrimes			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
1591553Srgrimes		endpwent();
1601553Srgrimes	}
1611553Srgrimes	setfsent();
1621553Srgrimes	while ((fs = getfsent()) != NULL) {
1631553Srgrimes		if (strcmp(fs->fs_vfstype, "ufs"))
1641553Srgrimes			continue;
1651553Srgrimes		if (aflag) {
166207736Smckusick			if (gflag)
167207736Smckusick				errs += repquota(fs, GRPQUOTA);
168207736Smckusick			if (uflag)
169207736Smckusick				errs += repquota(fs, USRQUOTA);
1701553Srgrimes			continue;
1711553Srgrimes		}
1721553Srgrimes		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
1731553Srgrimes		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
1741553Srgrimes			done |= 1 << argnum;
175207736Smckusick			if (gflag)
176207736Smckusick				errs += repquota(fs, GRPQUOTA);
177207736Smckusick			if (uflag)
178207736Smckusick				errs += repquota(fs, USRQUOTA);
1791553Srgrimes		}
1801553Srgrimes	}
1811553Srgrimes	endfsent();
1821553Srgrimes	for (i = 0; i < argc; i++)
1831553Srgrimes		if ((done & (1 << i)) == 0)
18430373Scharnier			warnx("%s not found in fstab", argv[i]);
1851553Srgrimes	exit(errs);
1861553Srgrimes}
1871553Srgrimes
18830373Scharnierstatic void
189180187Sdesusage(void)
1901553Srgrimes{
19130373Scharnier	fprintf(stderr, "%s\n%s\n",
192207736Smckusick		"usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
193207736Smckusick		"       repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
1941553Srgrimes	exit(1);
1951553Srgrimes}
1961553Srgrimes
19730373Scharnierint
198207736Smckusickrepquota(struct fstab *fs, int type)
1991553Srgrimes{
200180187Sdes	struct fileusage *fup;
201207736Smckusick	struct quotafile *qf;
202207736Smckusick	u_long id, maxid;
2031553Srgrimes	struct dqblk dqbuf;
2041553Srgrimes	static int multiple = 0;
2051553Srgrimes
206207736Smckusick	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
207207736Smckusick		if (vflag && !aflag) {
208207736Smckusick			if (multiple++)
209207736Smckusick				printf("\n");
210207736Smckusick			fprintf(stdout, "*** No %s quotas on %s (%s)\n",
211207736Smckusick			    qfextension[type], fs->fs_file, fs->fs_spec);
212207736Smckusick			return(1);
213207736Smckusick		}
214207736Smckusick		return(0);
2151553Srgrimes	}
2161553Srgrimes	if (multiple++)
2171553Srgrimes		printf("\n");
2181553Srgrimes	if (vflag)
2191553Srgrimes		fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
2201553Srgrimes		    qfextension[type], fs->fs_file, fs->fs_spec);
221207736Smckusick	printf("%*s           Block  limits                    File  limits\n",
222207736Smckusick		max(MAXLOGNAME - 1, 10), " ");
223207736Smckusick	printf("User%*s  used   soft   hard  grace     used    soft    hard  grace\n",
224207736Smckusick		max(MAXLOGNAME - 1, 10), " ");
225207736Smckusick	maxid = quota_maxid(qf);
226207736Smckusick	for (id = 0; id <= maxid; id++) {
227207736Smckusick		if (quota_read(qf, &dqbuf, id) != 0)
2281553Srgrimes			break;
2291553Srgrimes		if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
2301553Srgrimes			continue;
2311553Srgrimes		if ((fup = lookup(id, type)) == 0)
2321553Srgrimes			fup = addid(id, type, (char *)0);
233207736Smckusick		printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
234207736Smckusick		printf("%c%c",
235207736Smckusick		    dqbuf.dqb_bsoftlimit &&
236207736Smckusick		    dqbuf.dqb_curblocks >=
237207736Smckusick		    dqbuf.dqb_bsoftlimit ? '+' : '-',
238207736Smckusick		    dqbuf.dqb_isoftlimit &&
239207736Smckusick		    dqbuf.dqb_curinodes >=
240207736Smckusick		    dqbuf.dqb_isoftlimit ? '+' : '-');
241207736Smckusick		prthumanval(dqbuf.dqb_curblocks);
242207736Smckusick		prthumanval(dqbuf.dqb_bsoftlimit);
243207736Smckusick		prthumanval(dqbuf.dqb_bhardlimit);
244207736Smckusick		printf(" %6s",
245207736Smckusick		    dqbuf.dqb_bsoftlimit &&
246207736Smckusick		    dqbuf.dqb_curblocks >=
247207736Smckusick		    dqbuf.dqb_bsoftlimit ?
248207736Smckusick		    timeprt(dqbuf.dqb_btime) : "-");
249207736Smckusick		printf("  %7ju %7ju %7ju %6s\n",
250207736Smckusick		    (uintmax_t)dqbuf.dqb_curinodes,
251207736Smckusick		    (uintmax_t)dqbuf.dqb_isoftlimit,
252207736Smckusick		    (uintmax_t)dqbuf.dqb_ihardlimit,
253207736Smckusick		    dqbuf.dqb_isoftlimit &&
254207736Smckusick		    dqbuf.dqb_curinodes >=
255207736Smckusick		    dqbuf.dqb_isoftlimit ?
256207736Smckusick		    timeprt(dqbuf.dqb_itime) : "-");
2571553Srgrimes	}
258207736Smckusick	quota_close(qf);
2591553Srgrimes	return (0);
2601553Srgrimes}
2611553Srgrimes
262207736Smckusickstatic void
263207736Smckusickprthumanval(int64_t blocks)
264207736Smckusick{
265207736Smckusick	char buf[7];
266207736Smckusick	int flags;
267207736Smckusick
268207736Smckusick	if (!hflag) {
269207736Smckusick		printf(" %6ju", (uintmax_t)dbtokb(blocks));
270207736Smckusick		return;
271207736Smckusick	}
272207736Smckusick	flags = HN_NOSPACE | HN_DECIMAL;
273207736Smckusick	if (blocks != 0)
274207736Smckusick		flags |= HN_B;
275207736Smckusick	humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
276207736Smckusick	    dbtob(blocks), "", HN_AUTOSCALE, flags);
277207736Smckusick	(void)printf("%7s", buf);
278207736Smckusick}
279207736Smckusick
2801553Srgrimes/*
2811553Srgrimes * Check to see if target appears in list of size cnt.
2821553Srgrimes */
28330373Scharnierint
284180187Sdesoneof(char *target, char *list[], int cnt)
2851553Srgrimes{
286180187Sdes	int i;
2871553Srgrimes
2881553Srgrimes	for (i = 0; i < cnt; i++)
2891553Srgrimes		if (strcmp(target, list[i]) == 0)
2901553Srgrimes			return (i);
2911553Srgrimes	return (-1);
2921553Srgrimes}
2931553Srgrimes
2941553Srgrimes/*
2951553Srgrimes * Routines to manage the file usage table.
2961553Srgrimes *
2971553Srgrimes * Lookup an id of a specific type.
2981553Srgrimes */
2991553Srgrimesstruct fileusage *
300180187Sdeslookup(u_long id, int type)
3011553Srgrimes{
302180187Sdes	struct fileusage *fup;
3031553Srgrimes
3041553Srgrimes	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
3051553Srgrimes		if (fup->fu_id == id)
3061553Srgrimes			return (fup);
3071553Srgrimes	return ((struct fileusage *)0);
3081553Srgrimes}
3091553Srgrimes
3101553Srgrimes/*
3111553Srgrimes * Add a new file usage id if it does not already exist.
3121553Srgrimes */
3131553Srgrimesstruct fileusage *
314180187Sdesaddid(u_long id, int type, char *name)
3151553Srgrimes{
3161553Srgrimes	struct fileusage *fup, **fhp;
3171553Srgrimes	int len;
3181553Srgrimes
31930373Scharnier	if ((fup = lookup(id, type)))
3201553Srgrimes		return (fup);
3211553Srgrimes	if (name)
3221553Srgrimes		len = strlen(name);
3231553Srgrimes	else
3241553Srgrimes		len = 10;
32530373Scharnier	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
32630373Scharnier		errx(1, "out of memory for fileusage structures");
3271553Srgrimes	fhp = &fuhead[type][id & (FUHASH - 1)];
3281553Srgrimes	fup->fu_next = *fhp;
3291553Srgrimes	*fhp = fup;
3301553Srgrimes	fup->fu_id = id;
3311553Srgrimes	if (id > highid[type])
3321553Srgrimes		highid[type] = id;
3331553Srgrimes	if (name) {
3341553Srgrimes		bcopy(name, fup->fu_name, len + 1);
3351553Srgrimes	} else {
3368326Sbde		sprintf(fup->fu_name, "%lu", id);
3371553Srgrimes	}
3381553Srgrimes	return (fup);
3391553Srgrimes}
3401553Srgrimes
3411553Srgrimes/*
3421553Srgrimes * Calculate the grace period and return a printable string for it.
3431553Srgrimes */
3441553Srgrimeschar *
345180187Sdestimeprt(time_t seconds)
3461553Srgrimes{
3471553Srgrimes	time_t hours, minutes;
3481553Srgrimes	static char buf[20];
3491553Srgrimes	static time_t now;
3501553Srgrimes
3511553Srgrimes	if (now == 0)
3521553Srgrimes		time(&now);
35387597Smikeh	if (now > seconds) {
35487597Smikeh		strlcpy(buf, "none", sizeof (buf));
35587597Smikeh		return (buf);
35687597Smikeh	}
3571553Srgrimes	seconds -= now;
3581553Srgrimes	minutes = (seconds + 30) / 60;
3591553Srgrimes	hours = (minutes + 30) / 60;
3601553Srgrimes	if (hours >= 36) {
36187597Smikeh		sprintf(buf, "%lddays", (long)(hours + 12) / 24);
3621553Srgrimes		return (buf);
3631553Srgrimes	}
3641553Srgrimes	if (minutes >= 60) {
36587597Smikeh		sprintf(buf, "%2ld:%ld", (long)minutes / 60,
36687597Smikeh		    (long)minutes % 60);
3671553Srgrimes		return (buf);
3681553Srgrimes	}
36987597Smikeh	sprintf(buf, "%2ld", (long)minutes);
3701553Srgrimes	return (buf);
3711553Srgrimes}
372