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
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)repquota.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.sbin/repquota/repquota.c,v 1.9.2.2 2002/03/15 22:18:25 mikeh Exp $
44 #include <sys/param.h>
46 #include <vfs/ufs/quota.h>
58 /* Let's be paranoid about block size */
61 ((off_t)(db) >> (10-DEV_BSHIFT))
64 ((off_t)(db) << (DEV_BSHIFT-10))
66 #define dbtokb(db) (db)
69 #define max(a,b) ((a) >= (b) ? (a) : (b))
71 const char *qfname = QUOTAFILENAME;
72 const char *qfextension[] = INITQFNAMES;
75 struct fileusage *fu_next;
76 struct ufs_dqblk fu_dqblk;
81 #define FUHASH 1024 /* must be power of two */
82 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
83 struct fileusage *lookup(u_long, int);
84 struct fileusage *addid(u_long, int, char *);
85 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
87 int vflag; /* verbose */
88 int aflag; /* all file systems */
90 int hasquota(struct fstab *, int, char **);
91 int oneof(char *, char *[], int);
92 int repquota(struct fstab *, int, char *);
93 char *timeprt(time_t);
94 static void usage(void);
97 main(int argc, char **argv)
102 int gflag = 0, uflag = 0, errs = 0;
103 long i, argnum, done = 0;
106 while ((ch = getopt(argc, argv, "aguv")) != -1) {
126 if (argc == 0 && !aflag)
128 if (!gflag && !uflag) {
135 while ((gr = getgrent()) != NULL)
136 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
141 while ((pw = getpwent()) != NULL)
142 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
146 while ((fs = getfsent()) != NULL) {
147 if (strcmp(fs->fs_vfstype, "ufs"))
150 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
151 errs += repquota(fs, GRPQUOTA, qfnp);
152 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
153 errs += repquota(fs, USRQUOTA, qfnp);
156 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
157 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
159 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
160 errs += repquota(fs, GRPQUOTA, qfnp);
161 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
162 errs += repquota(fs, USRQUOTA, qfnp);
166 for (i = 0; i < argc; i++)
167 if ((done & (1 << i)) == 0)
168 warnx("%s not found in fstab", argv[i]);
175 fprintf(stderr, "%s\n%s\n",
176 "usage: repquota [-v] [-g] [-u] -a",
177 " repquota [-v] [-g] [-u] filesystem ...");
182 repquota(struct fstab *fs, int type, char *qfpathname)
184 struct fileusage *fup;
187 struct ufs_dqblk dqbuf;
188 static struct ufs_dqblk zerodqblk;
189 static int warned = 0;
190 static int multiple = 0;
192 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
193 errno == EOPNOTSUPP && !warned && vflag) {
196 "*** Warning: Quotas are not compiled into this kernel\n");
201 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
202 qfextension[type], fs->fs_file, fs->fs_spec);
203 if ((qf = fopen(qfpathname, "r")) == NULL) {
204 warn("%s", qfpathname);
207 for (id = 0; ; id++) {
208 fread(&dqbuf, sizeof(struct ufs_dqblk), 1, qf);
211 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
213 if ((fup = lookup(id, type)) == NULL)
214 fup = addid(id, type, NULL);
215 fup->fu_dqblk = dqbuf;
218 printf("%*s Block limits File limits\n",
219 max(UT_NAMESIZE,10), " ");
220 printf("User%*s used soft hard grace used soft hard grace\n",
221 max(UT_NAMESIZE,10), " ");
222 for (id = 0; id <= highid[type]; id++) {
223 fup = lookup(id, type);
226 if (fup->fu_dqblk.dqb_curinodes == 0 &&
227 fup->fu_dqblk.dqb_curblocks == 0)
229 printf("%-*s", max(UT_NAMESIZE,10), fup->fu_name);
230 printf("%c%c %8lu %8lu %8lu %6s",
231 fup->fu_dqblk.dqb_bsoftlimit &&
232 fup->fu_dqblk.dqb_curblocks >=
233 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
234 fup->fu_dqblk.dqb_isoftlimit &&
235 fup->fu_dqblk.dqb_curinodes >=
236 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
237 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
238 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
239 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
240 fup->fu_dqblk.dqb_bsoftlimit &&
241 fup->fu_dqblk.dqb_curblocks >=
242 fup->fu_dqblk.dqb_bsoftlimit ?
243 timeprt(fup->fu_dqblk.dqb_btime) : "-");
244 printf(" %7lu %7lu %7lu %6s\n",
245 (u_long)fup->fu_dqblk.dqb_curinodes,
246 (u_long)fup->fu_dqblk.dqb_isoftlimit,
247 (u_long)fup->fu_dqblk.dqb_ihardlimit,
248 fup->fu_dqblk.dqb_isoftlimit &&
249 fup->fu_dqblk.dqb_curinodes >=
250 fup->fu_dqblk.dqb_isoftlimit ?
251 timeprt(fup->fu_dqblk.dqb_itime) : "-");
252 fup->fu_dqblk = zerodqblk;
258 * Check to see if target appears in list of size cnt.
261 oneof(char *target, char *list[], int cnt)
265 for (i = 0; i < cnt; i++)
266 if (strcmp(target, list[i]) == 0)
272 * Check to see if a particular quota is to be enabled.
275 hasquota(struct fstab *fs, int type, char **qfnamep)
279 static char initname, usrname[100], grpname[100];
280 static char buf[BUFSIZ];
283 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
284 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
287 strcpy(buf, fs->fs_mntops);
288 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
289 if ((cp = strchr(opt, '=')))
291 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
293 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
302 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
308 * Routines to manage the file usage table.
310 * Lookup an id of a specific type.
313 lookup(u_long id, int type)
315 struct fileusage *fup;
317 for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
318 if (fup->fu_id == id)
324 * Add a new file usage id if it does not already exist.
327 addid(u_long id, int type, char *name)
329 struct fileusage *fup, **fhp;
332 if ((fup = lookup(id, type)))
338 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
339 errx(1, "out of memory for fileusage structures");
340 fhp = &fuhead[type][id & (FUHASH - 1)];
344 if (id > highid[type])
347 bcopy(name, fup->fu_name, len + 1);
349 sprintf(fup->fu_name, "%lu", id);
355 * Calculate the grace period and return a printable string for it.
358 timeprt(time_t seconds)
360 time_t hours, minutes;
367 strlcpy(buf, "none", sizeof (buf));
371 minutes = (seconds + 30) / 60;
372 hours = (minutes + 30) / 60;
374 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
378 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
382 sprintf(buf, "%2ld", (long)minutes);