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
32 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)repquota.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/repquota/repquota.c,v 1.9.2.2 2002/03/15 22:18:25 mikeh Exp $
40 #include <sys/param.h>
42 #include <vfs/ufs/quota.h>
54 /* Let's be paranoid about block size */
57 ((off_t)(db) >> (10-DEV_BSHIFT))
60 ((off_t)(db) << (DEV_BSHIFT-10))
62 #define dbtokb(db) (db)
65 #define max(a,b) ((a) >= (b) ? (a) : (b))
67 const char *qfname = QUOTAFILENAME;
68 const char *qfextension[] = INITQFNAMES;
71 struct fileusage *fu_next;
72 struct ufs_dqblk fu_dqblk;
77 #define FUHASH 1024 /* must be power of two */
78 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
79 struct fileusage *lookup(u_long, int);
80 struct fileusage *addid(u_long, int, char *);
81 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
83 int vflag; /* verbose */
84 int aflag; /* all file systems */
86 int hasquota(struct fstab *, int, char **);
87 int oneof(char *, char *[], int);
88 int repquota(struct fstab *, int, char *);
89 char *timeprt(time_t);
90 static void usage(void);
93 main(int argc, char **argv)
98 int gflag = 0, uflag = 0, errs = 0;
99 long i, argnum, done = 0;
102 while ((ch = getopt(argc, argv, "aguv")) != -1) {
122 if (argc == 0 && !aflag)
124 if (!gflag && !uflag) {
131 while ((gr = getgrent()) != NULL)
132 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
137 while ((pw = getpwent()) != NULL)
138 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
142 while ((fs = getfsent()) != NULL) {
143 if (strcmp(fs->fs_vfstype, "ufs"))
146 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
147 errs += repquota(fs, GRPQUOTA, qfnp);
148 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
149 errs += repquota(fs, USRQUOTA, qfnp);
152 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
153 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
155 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
156 errs += repquota(fs, GRPQUOTA, qfnp);
157 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
158 errs += repquota(fs, USRQUOTA, qfnp);
162 for (i = 0; i < argc; i++)
163 if ((done & (1 << i)) == 0)
164 warnx("%s not found in fstab", argv[i]);
171 fprintf(stderr, "%s\n%s\n",
172 "usage: repquota [-v] [-g] [-u] -a",
173 " repquota [-v] [-g] [-u] filesystem ...");
178 repquota(struct fstab *fs, int type, char *qfpathname)
180 struct fileusage *fup;
183 struct ufs_dqblk dqbuf;
184 static struct ufs_dqblk zerodqblk;
185 static int warned = 0;
186 static int multiple = 0;
188 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
189 errno == EOPNOTSUPP && !warned && vflag) {
192 "*** Warning: Quotas are not compiled into this kernel\n");
197 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
198 qfextension[type], fs->fs_file, fs->fs_spec);
199 if ((qf = fopen(qfpathname, "r")) == NULL) {
200 warn("%s", qfpathname);
203 for (id = 0; ; id++) {
204 fread(&dqbuf, sizeof(struct ufs_dqblk), 1, qf);
207 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
209 if ((fup = lookup(id, type)) == NULL)
210 fup = addid(id, type, NULL);
211 fup->fu_dqblk = dqbuf;
214 printf("%*s Block limits File limits\n",
215 max(UT_NAMESIZE,10), " ");
216 printf("User%*s used soft hard grace used soft hard grace\n",
217 max(UT_NAMESIZE,10), " ");
218 for (id = 0; id <= highid[type]; id++) {
219 fup = lookup(id, type);
222 if (fup->fu_dqblk.dqb_curinodes == 0 &&
223 fup->fu_dqblk.dqb_curblocks == 0)
225 printf("%-*s", max(UT_NAMESIZE,10), fup->fu_name);
226 printf("%c%c %8lu %8lu %8lu %6s",
227 fup->fu_dqblk.dqb_bsoftlimit &&
228 fup->fu_dqblk.dqb_curblocks >=
229 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
230 fup->fu_dqblk.dqb_isoftlimit &&
231 fup->fu_dqblk.dqb_curinodes >=
232 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
233 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
234 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
235 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
236 fup->fu_dqblk.dqb_bsoftlimit &&
237 fup->fu_dqblk.dqb_curblocks >=
238 fup->fu_dqblk.dqb_bsoftlimit ?
239 timeprt(fup->fu_dqblk.dqb_btime) : "-");
240 printf(" %7lu %7lu %7lu %6s\n",
241 (u_long)fup->fu_dqblk.dqb_curinodes,
242 (u_long)fup->fu_dqblk.dqb_isoftlimit,
243 (u_long)fup->fu_dqblk.dqb_ihardlimit,
244 fup->fu_dqblk.dqb_isoftlimit &&
245 fup->fu_dqblk.dqb_curinodes >=
246 fup->fu_dqblk.dqb_isoftlimit ?
247 timeprt(fup->fu_dqblk.dqb_itime) : "-");
248 fup->fu_dqblk = zerodqblk;
254 * Check to see if target appears in list of size cnt.
257 oneof(char *target, char *list[], int cnt)
261 for (i = 0; i < cnt; i++)
262 if (strcmp(target, list[i]) == 0)
268 * Check to see if a particular quota is to be enabled.
271 hasquota(struct fstab *fs, int type, char **qfnamep)
275 static char initname, usrname[100], grpname[100];
276 static char buf[BUFSIZ];
279 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
280 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
283 strcpy(buf, fs->fs_mntops);
284 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
285 if ((cp = strchr(opt, '=')))
287 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
289 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
298 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
304 * Routines to manage the file usage table.
306 * Lookup an id of a specific type.
309 lookup(u_long id, int type)
311 struct fileusage *fup;
313 for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
314 if (fup->fu_id == id)
320 * Add a new file usage id if it does not already exist.
323 addid(u_long id, int type, char *name)
325 struct fileusage *fup, **fhp;
328 if ((fup = lookup(id, type)))
334 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
335 errx(1, "out of memory for fileusage structures");
336 fhp = &fuhead[type][id & (FUHASH - 1)];
340 if (id > highid[type])
343 bcopy(name, fup->fu_name, len + 1);
345 sprintf(fup->fu_name, "%lu", id);
351 * Calculate the grace period and return a printable string for it.
354 timeprt(time_t seconds)
356 time_t hours, minutes;
363 strlcpy(buf, "none", sizeof (buf));
367 minutes = (seconds + 30) / 60;
368 hours = (minutes + 30) / 60;
370 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
374 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
378 sprintf(buf, "%2ld", (long)minutes);