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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
48 "$FreeBSD: src/usr.sbin/repquota/repquota.c,v 1.9.2.2 2002/03/15 22:18:25 mikeh Exp $";
54 #include <sys/param.h>
56 #include <ufs/ufs/quota.h>
68 /* Let's be paranoid about block size */
71 ((off_t)(db) >> (10-DEV_BSHIFT))
74 ((off_t)(db) << (DEV_BSHIFT-10))
76 #define dbtokb(db) (db)
79 #define max(a,b) ((a) >= (b) ? (a) : (b))
81 const char *qfname = QUOTAFILENAME;
82 const char *qfextension[] = INITQFNAMES;
85 struct fileusage *fu_next;
86 struct dqblk fu_dqblk;
91 #define FUHASH 1024 /* must be power of two */
92 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
93 struct fileusage *lookup(u_long, int);
94 struct fileusage *addid(u_long, int, char *);
95 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
97 int vflag; /* verbose */
98 int aflag; /* all file systems */
100 int hasquota __P((struct fstab *, int, char **));
101 int oneof __P((char *, char *[], int));
102 int repquota __P((struct fstab *, int, char *));
103 char *timeprt __P((time_t));
104 static void usage __P((void));
111 register struct fstab *fs;
112 register struct passwd *pw;
113 register struct group *gr;
114 int gflag = 0, uflag = 0, errs = 0;
115 long i, argnum, done = 0;
118 while ((ch = getopt(argc, argv, "aguv")) != -1) {
138 if (argc == 0 && !aflag)
140 if (!gflag && !uflag) {
147 while ((gr = getgrent()) != 0)
148 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
153 while ((pw = getpwent()) != 0)
154 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
158 while ((fs = getfsent()) != NULL) {
159 if (strcmp(fs->fs_vfstype, "ufs"))
162 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
163 errs += repquota(fs, GRPQUOTA, qfnp);
164 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
165 errs += repquota(fs, USRQUOTA, qfnp);
168 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
169 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
171 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
172 errs += repquota(fs, GRPQUOTA, qfnp);
173 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
174 errs += repquota(fs, USRQUOTA, qfnp);
178 for (i = 0; i < argc; i++)
179 if ((done & (1 << i)) == 0)
180 warnx("%s not found in fstab", argv[i]);
187 fprintf(stderr, "%s\n%s\n",
188 "usage: repquota [-v] [-g] [-u] -a",
189 " repquota [-v] [-g] [-u] filesystem ...");
194 repquota(fs, type, qfpathname)
195 register struct fstab *fs;
199 register struct fileusage *fup;
203 static struct dqblk zerodqblk;
204 static int warned = 0;
205 static int multiple = 0;
207 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
208 errno == EOPNOTSUPP && !warned && vflag) {
211 "*** Warning: Quotas are not compiled into this kernel\n");
216 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
217 qfextension[type], fs->fs_file, fs->fs_spec);
218 if ((qf = fopen(qfpathname, "r")) == NULL) {
219 warn("%s", qfpathname);
222 for (id = 0; ; id++) {
223 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
226 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
228 if ((fup = lookup(id, type)) == 0)
229 fup = addid(id, type, (char *)0);
230 fup->fu_dqblk = dqbuf;
233 printf("%*s Block limits File limits\n",
234 max(UT_NAMESIZE,10), " ");
235 printf("User%*s used soft hard grace used soft hard grace\n",
236 max(UT_NAMESIZE,10), " ");
237 for (id = 0; id <= highid[type]; id++) {
238 fup = lookup(id, type);
241 if (fup->fu_dqblk.dqb_curinodes == 0 &&
242 fup->fu_dqblk.dqb_curblocks == 0)
244 printf("%-*s", max(UT_NAMESIZE,10), fup->fu_name);
245 printf("%c%c %8lu %8lu %8lu %6s",
246 fup->fu_dqblk.dqb_bsoftlimit &&
247 fup->fu_dqblk.dqb_curblocks >=
248 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
249 fup->fu_dqblk.dqb_isoftlimit &&
250 fup->fu_dqblk.dqb_curinodes >=
251 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
252 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
253 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
254 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
255 fup->fu_dqblk.dqb_bsoftlimit &&
256 fup->fu_dqblk.dqb_curblocks >=
257 fup->fu_dqblk.dqb_bsoftlimit ?
258 timeprt(fup->fu_dqblk.dqb_btime) : "-");
259 printf(" %7lu %7lu %7lu %6s\n",
260 (u_long)fup->fu_dqblk.dqb_curinodes,
261 (u_long)fup->fu_dqblk.dqb_isoftlimit,
262 (u_long)fup->fu_dqblk.dqb_ihardlimit,
263 fup->fu_dqblk.dqb_isoftlimit &&
264 fup->fu_dqblk.dqb_curinodes >=
265 fup->fu_dqblk.dqb_isoftlimit ?
266 timeprt(fup->fu_dqblk.dqb_itime) : "-");
267 fup->fu_dqblk = zerodqblk;
273 * Check to see if target appears in list of size cnt.
276 oneof(target, list, cnt)
277 register char *target, *list[];
282 for (i = 0; i < cnt; i++)
283 if (strcmp(target, list[i]) == 0)
289 * Check to see if a particular quota is to be enabled.
292 hasquota(fs, type, qfnamep)
293 register struct fstab *fs;
299 static char initname, usrname[100], grpname[100];
300 static char buf[BUFSIZ];
303 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
304 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
307 strcpy(buf, fs->fs_mntops);
308 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
309 if ((cp = index(opt, '=')))
311 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
313 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
322 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
328 * Routines to manage the file usage table.
330 * Lookup an id of a specific type.
337 register struct fileusage *fup;
339 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
340 if (fup->fu_id == id)
342 return ((struct fileusage *)0);
346 * Add a new file usage id if it does not already exist.
349 addid(id, type, name)
354 struct fileusage *fup, **fhp;
357 if ((fup = lookup(id, type)))
363 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
364 errx(1, "out of memory for fileusage structures");
365 fhp = &fuhead[type][id & (FUHASH - 1)];
369 if (id > highid[type])
372 bcopy(name, fup->fu_name, len + 1);
374 sprintf(fup->fu_name, "%lu", id);
380 * Calculate the grace period and return a printable string for it.
386 time_t hours, minutes;
393 strlcpy(buf, "none", sizeof (buf));
397 minutes = (seconds + 30) / 60;
398 hours = (minutes + 30) / 60;
400 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
404 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
408 sprintf(buf, "%2ld", (long)minutes);