2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <ufs/ufs/quota.h>
64 /* Let's be paranoid about block size */
67 ((off_t)(db) >> (10-DEV_BSHIFT))
70 ((off_t)(db) << (DEV_BSHIFT-10))
72 #define dbtokb(db) (db)
75 #define max(a,b) ((a) >= (b) ? (a) : (b))
77 const char *qfname = QUOTAFILENAME;
78 const char *qfextension[] = INITQFNAMES;
81 struct fileusage *fu_next;
82 struct dqblk fu_dqblk;
87 #define FUHASH 1024 /* must be power of two */
88 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
89 struct fileusage *lookup(u_long, int);
90 struct fileusage *addid(u_long, int, char *);
91 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
93 int vflag; /* verbose */
94 int aflag; /* all filesystems */
95 int nflag; /* display user/group by id */
97 int hasquota(struct fstab *, int, char **);
98 int oneof(char *, char *[], int);
99 int repquota(struct fstab *, int, char *);
100 char *timeprt(time_t);
101 static void usage(void);
104 main(int argc, char **argv)
106 register struct fstab *fs;
107 register struct passwd *pw;
108 register struct group *gr;
109 int ch, gflag = 0, uflag = 0, errs = 0;
110 long i, argnum, done = 0;
113 while ((ch = getopt(argc, argv, "agnuv")) != -1) {
136 if (argc == 0 && !aflag)
138 if (!gflag && !uflag) {
143 if (gflag && !nflag) {
145 while ((gr = getgrent()) != 0)
146 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
149 if (uflag && !nflag) {
151 while ((pw = getpwent()) != 0)
152 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
156 while ((fs = getfsent()) != NULL) {
157 if (strcmp(fs->fs_vfstype, "ufs"))
160 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
161 errs += repquota(fs, GRPQUOTA, qfnp);
162 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
163 errs += repquota(fs, USRQUOTA, qfnp);
166 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
167 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
169 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
170 errs += repquota(fs, GRPQUOTA, qfnp);
171 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
172 errs += repquota(fs, USRQUOTA, qfnp);
176 for (i = 0; i < argc; i++)
177 if ((done & (1 << i)) == 0)
178 warnx("%s not found in fstab", argv[i]);
185 fprintf(stderr, "%s\n%s\n",
186 "usage: repquota [-v] [-g] [-n] [-u] -a",
187 " repquota [-v] [-g] [-n] [-u] filesystem ...");
192 repquota(fs, type, qfpathname)
193 register struct fstab *fs;
197 register struct fileusage *fup;
201 static struct dqblk zerodqblk;
202 static int warned = 0;
203 static int multiple = 0;
205 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
206 errno == EOPNOTSUPP && !warned && vflag) {
209 "*** Warning: Quotas are not compiled into this kernel\n");
214 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
215 qfextension[type], fs->fs_file, fs->fs_spec);
216 if ((qf = fopen(qfpathname, "r")) == NULL) {
217 warn("%s", qfpathname);
220 for (id = 0; ; id++) {
221 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
224 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
226 if ((fup = lookup(id, type)) == 0)
227 fup = addid(id, type, (char *)0);
228 fup->fu_dqblk = dqbuf;
231 printf("%*s Block limits File limits\n",
232 max(UT_NAMESIZE,10), " ");
233 printf("User%*s used soft hard grace used soft hard grace\n",
234 max(UT_NAMESIZE,10), " ");
235 for (id = 0; id <= highid[type]; id++) {
236 fup = lookup(id, type);
239 if (fup->fu_dqblk.dqb_curinodes == 0 &&
240 fup->fu_dqblk.dqb_curblocks == 0)
242 printf("%-*s ", max(UT_NAMESIZE,10), fup->fu_name);
243 printf("%c%c %8lu %8lu %8lu %6s",
244 fup->fu_dqblk.dqb_bsoftlimit &&
245 fup->fu_dqblk.dqb_curblocks >=
246 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
247 fup->fu_dqblk.dqb_isoftlimit &&
248 fup->fu_dqblk.dqb_curinodes >=
249 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
250 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
251 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
252 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
253 fup->fu_dqblk.dqb_bsoftlimit &&
254 fup->fu_dqblk.dqb_curblocks >=
255 fup->fu_dqblk.dqb_bsoftlimit ?
256 timeprt(fup->fu_dqblk.dqb_btime) : "-");
257 printf(" %7lu %7lu %7lu %6s\n",
258 (u_long)fup->fu_dqblk.dqb_curinodes,
259 (u_long)fup->fu_dqblk.dqb_isoftlimit,
260 (u_long)fup->fu_dqblk.dqb_ihardlimit,
261 fup->fu_dqblk.dqb_isoftlimit &&
262 fup->fu_dqblk.dqb_curinodes >=
263 fup->fu_dqblk.dqb_isoftlimit ?
264 timeprt(fup->fu_dqblk.dqb_itime) : "-");
265 fup->fu_dqblk = zerodqblk;
271 * Check to see if target appears in list of size cnt.
274 oneof(target, list, cnt)
275 register char *target, *list[];
280 for (i = 0; i < cnt; i++)
281 if (strcmp(target, list[i]) == 0)
287 * Check to see if a particular quota is to be enabled.
290 hasquota(fs, type, qfnamep)
291 register struct fstab *fs;
297 static char initname, usrname[100], grpname[100];
298 static char buf[BUFSIZ];
301 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
302 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
305 strcpy(buf, fs->fs_mntops);
306 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
307 if ((cp = index(opt, '=')))
309 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
311 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
320 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
326 * Routines to manage the file usage table.
328 * Lookup an id of a specific type.
335 register struct fileusage *fup;
337 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
338 if (fup->fu_id == id)
340 return ((struct fileusage *)0);
344 * Add a new file usage id if it does not already exist.
347 addid(id, type, name)
352 struct fileusage *fup, **fhp;
355 if ((fup = lookup(id, type)))
361 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
362 errx(1, "out of memory for fileusage structures");
363 fhp = &fuhead[type][id & (FUHASH - 1)];
367 if (id > highid[type])
370 bcopy(name, fup->fu_name, len + 1);
372 sprintf(fup->fu_name, "%lu", id);
378 * Calculate the grace period and return a printable string for it.
384 time_t hours, minutes;
391 strlcpy(buf, "none", sizeof (buf));
395 minutes = (seconds + 30) / 60;
396 hours = (minutes + 30) / 60;
398 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
402 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
406 sprintf(buf, "%2ld", (long)minutes);