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 <sys/mount.h>
52 #include <ufs/ufs/quota.h>
65 /* Let's be paranoid about block size */
68 ((off_t)(db) >> (10-DEV_BSHIFT))
71 ((off_t)(db) << (DEV_BSHIFT-10))
73 #define dbtokb(db) (db)
76 #define max(a,b) ((a) >= (b) ? (a) : (b))
78 const char *qfname = QUOTAFILENAME;
79 const char *qfextension[] = INITQFNAMES;
82 struct fileusage *fu_next;
83 struct dqblk fu_dqblk;
88 #define FUHASH 1024 /* must be power of two */
89 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
90 struct fileusage *lookup(u_long, int);
91 struct fileusage *addid(u_long, int, char *);
92 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
94 int vflag; /* verbose */
95 int aflag; /* all filesystems */
96 int nflag; /* display user/group by id */
98 int hasquota(struct fstab *, int, char **);
99 int oneof(char *, char *[], int);
100 int repquota(struct fstab *, int, char *);
101 char *timeprt(time_t);
102 static void usage(void);
105 main(int argc, char *argv[])
110 int ch, gflag = 0, uflag = 0, errs = 0;
111 long i, argnum, done = 0;
114 while ((ch = getopt(argc, argv, "agnuv")) != -1) {
137 if (argc == 0 && !aflag)
139 if (!gflag && !uflag) {
144 if (gflag && !nflag) {
146 while ((gr = getgrent()) != 0)
147 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
150 if (uflag && !nflag) {
152 while ((pw = getpwent()) != 0)
153 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
157 while ((fs = getfsent()) != NULL) {
158 if (strcmp(fs->fs_vfstype, "ufs"))
161 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
162 errs += repquota(fs, GRPQUOTA, qfnp);
163 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
164 errs += repquota(fs, USRQUOTA, qfnp);
167 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
168 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
170 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
171 errs += repquota(fs, GRPQUOTA, qfnp);
172 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
173 errs += repquota(fs, USRQUOTA, qfnp);
177 for (i = 0; i < argc; i++)
178 if ((done & (1 << i)) == 0)
179 warnx("%s not found in fstab", argv[i]);
186 fprintf(stderr, "%s\n%s\n",
187 "usage: repquota [-v] [-g] [-n] [-u] -a",
188 " repquota [-v] [-g] [-n] [-u] filesystem ...");
193 repquota(struct fstab *fs, int type, char *qfpathname)
195 struct fileusage *fup;
199 static struct dqblk zerodqblk;
200 static int warned = 0;
201 static int multiple = 0;
203 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
204 errno == EOPNOTSUPP && !warned && vflag) {
207 "*** Warning: Quotas are not compiled into this kernel\n");
212 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
213 qfextension[type], fs->fs_file, fs->fs_spec);
214 if ((qf = fopen(qfpathname, "r")) == NULL) {
215 warn("%s", qfpathname);
218 for (id = 0; ; id++) {
219 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
222 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
224 if ((fup = lookup(id, type)) == 0)
225 fup = addid(id, type, (char *)0);
226 fup->fu_dqblk = dqbuf;
229 printf("%*s Block limits File limits\n",
230 max(UT_NAMESIZE,10), " ");
231 printf("%s%*s used soft hard grace used soft hard grace\n",
232 type == USRQUOTA ? "User " : "Group", max(UT_NAMESIZE,10), " ");
233 for (id = 0; id <= highid[type]; id++) {
234 fup = lookup(id, type);
237 if (fup->fu_dqblk.dqb_curinodes == 0 &&
238 fup->fu_dqblk.dqb_curblocks == 0)
240 printf("%-*s ", max(UT_NAMESIZE,10), fup->fu_name);
241 printf("%c%c %8lu %8lu %8lu %6s",
242 fup->fu_dqblk.dqb_bsoftlimit &&
243 fup->fu_dqblk.dqb_curblocks >=
244 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
245 fup->fu_dqblk.dqb_isoftlimit &&
246 fup->fu_dqblk.dqb_curinodes >=
247 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
248 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
249 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
250 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
251 fup->fu_dqblk.dqb_bsoftlimit &&
252 fup->fu_dqblk.dqb_curblocks >=
253 fup->fu_dqblk.dqb_bsoftlimit ?
254 timeprt(fup->fu_dqblk.dqb_btime) : "-");
255 printf(" %7lu %7lu %7lu %6s\n",
256 (u_long)fup->fu_dqblk.dqb_curinodes,
257 (u_long)fup->fu_dqblk.dqb_isoftlimit,
258 (u_long)fup->fu_dqblk.dqb_ihardlimit,
259 fup->fu_dqblk.dqb_isoftlimit &&
260 fup->fu_dqblk.dqb_curinodes >=
261 fup->fu_dqblk.dqb_isoftlimit ?
262 timeprt(fup->fu_dqblk.dqb_itime) : "-");
263 fup->fu_dqblk = zerodqblk;
269 * Check to see if target appears in list of size cnt.
272 oneof(char *target, char *list[], int cnt)
276 for (i = 0; i < cnt; i++)
277 if (strcmp(target, list[i]) == 0)
283 * Check to see if a particular quota is to be enabled.
286 hasquota(struct fstab *fs, int type, char **qfnamep)
291 static char initname, usrname[100], grpname[100];
292 static char buf[BUFSIZ];
295 (void)snprintf(usrname, sizeof(usrname), "%s%s",
296 qfextension[USRQUOTA], qfname);
297 (void)snprintf(grpname, sizeof(grpname), "%s%s",
298 qfextension[GRPQUOTA], qfname);
301 strcpy(buf, fs->fs_mntops);
302 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
303 if ((cp = index(opt, '=')))
305 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
307 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
315 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
316 qfname, qfextension[type]);
319 if (statfs(fs->fs_file, &sfb) != 0) {
320 warn("cannot statfs mount point %s", fs->fs_file);
323 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
324 warnx("%s not mounted for %s quotas", fs->fs_file,
325 type == USRQUOTA ? "user" : "group");
332 * Routines to manage the file usage table.
334 * Lookup an id of a specific type.
337 lookup(u_long id, int type)
339 struct fileusage *fup;
341 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
342 if (fup->fu_id == id)
344 return ((struct fileusage *)0);
348 * Add a new file usage id if it does not already exist.
351 addid(u_long id, int type, char *name)
353 struct fileusage *fup, **fhp;
356 if ((fup = lookup(id, type)))
362 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
363 errx(1, "out of memory for fileusage structures");
364 fhp = &fuhead[type][id & (FUHASH - 1)];
368 if (id > highid[type])
371 bcopy(name, fup->fu_name, len + 1);
373 sprintf(fup->fu_name, "%lu", id);
379 * Calculate the grace period and return a printable string for it.
382 timeprt(time_t seconds)
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);