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. 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>
53 /* Let's be paranoid about block size */
56 ((off_t)(db) >> (10-DEV_BSHIFT))
59 ((off_t)(db) << (DEV_BSHIFT-10))
61 #define dbtokb(db) (db)
64 #define max(a,b) ((a) >= (b) ? (a) : (b))
66 const char *qfname = QUOTAFILENAME;
67 const char *qfextension[] = INITQFNAMES;
70 struct fileusage *fu_next;
71 struct ufs_dqblk fu_dqblk;
76 #define FUHASH 1024 /* must be power of two */
77 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
78 struct fileusage *lookup(u_long, int);
79 struct fileusage *addid(u_long, int, char *);
80 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
82 int vflag; /* verbose */
83 int aflag; /* all file systems */
85 int hasquota(struct fstab *, int, char **);
86 int oneof(char *, char *[], int);
87 int repquota(struct fstab *, int, char *);
88 char *timeprt(time_t);
89 static void usage(void);
92 main(int argc, char **argv)
97 int gflag = 0, uflag = 0, errs = 0;
98 long i, argnum, done = 0;
101 while ((ch = getopt(argc, argv, "aguv")) != -1) {
121 if (argc == 0 && !aflag)
123 if (!gflag && !uflag) {
130 while ((gr = getgrent()) != NULL)
131 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
136 while ((pw = getpwent()) != NULL)
137 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
141 while ((fs = getfsent()) != NULL) {
142 if (strcmp(fs->fs_vfstype, "ufs"))
145 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
146 errs += repquota(fs, GRPQUOTA, qfnp);
147 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
148 errs += repquota(fs, USRQUOTA, qfnp);
151 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
152 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
154 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
155 errs += repquota(fs, GRPQUOTA, qfnp);
156 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
157 errs += repquota(fs, USRQUOTA, qfnp);
161 for (i = 0; i < argc; i++)
162 if ((done & (1 << i)) == 0)
163 warnx("%s not found in fstab", argv[i]);
170 fprintf(stderr, "%s\n%s\n",
171 "usage: repquota [-v] [-g] [-u] -a",
172 " repquota [-v] [-g] [-u] filesystem ...");
177 repquota(struct fstab *fs, int type, char *qfpathname)
179 struct fileusage *fup;
182 struct ufs_dqblk dqbuf;
183 static struct ufs_dqblk zerodqblk;
184 static int warned = 0;
185 static int multiple = 0;
187 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
188 errno == EOPNOTSUPP && !warned && vflag) {
191 "*** Warning: Quotas are not compiled into this kernel\n");
196 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
197 qfextension[type], fs->fs_file, fs->fs_spec);
198 if ((qf = fopen(qfpathname, "r")) == NULL) {
199 warn("%s", qfpathname);
202 for (id = 0; ; id++) {
203 fread(&dqbuf, sizeof(struct ufs_dqblk), 1, qf);
206 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
208 if ((fup = lookup(id, type)) == NULL)
209 fup = addid(id, type, NULL);
210 fup->fu_dqblk = dqbuf;
213 printf("%*s Block limits File limits\n",
214 max(MAXLOGNAME - 1, 10), " ");
215 printf("User%*s used soft hard grace used soft hard grace\n",
216 max(MAXLOGNAME - 1, 10), " ");
217 for (id = 0; id <= highid[type]; id++) {
218 fup = lookup(id, type);
221 if (fup->fu_dqblk.dqb_curinodes == 0 &&
222 fup->fu_dqblk.dqb_curblocks == 0)
224 printf("%-*s", max(MAXLOGNAME - 1, 10), fup->fu_name);
225 printf("%c%c %8lu %8lu %8lu %6s",
226 fup->fu_dqblk.dqb_bsoftlimit &&
227 fup->fu_dqblk.dqb_curblocks >=
228 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
229 fup->fu_dqblk.dqb_isoftlimit &&
230 fup->fu_dqblk.dqb_curinodes >=
231 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
232 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
233 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
234 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
235 fup->fu_dqblk.dqb_bsoftlimit &&
236 fup->fu_dqblk.dqb_curblocks >=
237 fup->fu_dqblk.dqb_bsoftlimit ?
238 timeprt(fup->fu_dqblk.dqb_btime) : "-");
239 printf(" %7lu %7lu %7lu %6s\n",
240 (u_long)fup->fu_dqblk.dqb_curinodes,
241 (u_long)fup->fu_dqblk.dqb_isoftlimit,
242 (u_long)fup->fu_dqblk.dqb_ihardlimit,
243 fup->fu_dqblk.dqb_isoftlimit &&
244 fup->fu_dqblk.dqb_curinodes >=
245 fup->fu_dqblk.dqb_isoftlimit ?
246 timeprt(fup->fu_dqblk.dqb_itime) : "-");
247 fup->fu_dqblk = zerodqblk;
253 * Check to see if target appears in list of size cnt.
256 oneof(char *target, char *list[], int cnt)
260 for (i = 0; i < cnt; i++)
261 if (strcmp(target, list[i]) == 0)
267 * Check to see if a particular quota is to be enabled.
270 hasquota(struct fstab *fs, int type, char **qfnamep)
274 static char initname, usrname[100], grpname[100];
275 static char buf[BUFSIZ];
278 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
279 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
282 strcpy(buf, fs->fs_mntops);
283 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
284 if ((cp = strchr(opt, '=')))
286 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
288 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
297 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
303 * Routines to manage the file usage table.
305 * Lookup an id of a specific type.
308 lookup(u_long id, int type)
310 struct fileusage *fup;
312 for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
313 if (fup->fu_id == id)
319 * Add a new file usage id if it does not already exist.
322 addid(u_long id, int type, char *name)
324 struct fileusage *fup, **fhp;
327 if ((fup = lookup(id, type)))
333 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
334 errx(1, "out of memory for fileusage structures");
335 fhp = &fuhead[type][id & (FUHASH - 1)];
339 if (id > highid[type])
342 bcopy(name, fup->fu_name, len + 1);
344 sprintf(fup->fu_name, "%lu", id);
350 * Calculate the grace period and return a printable string for it.
353 timeprt(time_t seconds)
355 time_t hours, minutes;
362 strlcpy(buf, "none", sizeof (buf));
366 minutes = (seconds + 30) / 60;
367 hours = (minutes + 30) / 60;
369 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
373 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
377 sprintf(buf, "%2ld", (long)minutes);