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)
107 register struct fstab *fs;
108 register struct passwd *pw;
109 register struct group *gr;
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(fs, type, qfpathname)
194 register struct fstab *fs;
198 register struct fileusage *fup;
202 static struct dqblk zerodqblk;
203 static int warned = 0;
204 static int multiple = 0;
206 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
207 errno == EOPNOTSUPP && !warned && vflag) {
210 "*** Warning: Quotas are not compiled into this kernel\n");
215 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
216 qfextension[type], fs->fs_file, fs->fs_spec);
217 if ((qf = fopen(qfpathname, "r")) == NULL) {
218 warn("%s", qfpathname);
221 for (id = 0; ; id++) {
222 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
225 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
227 if ((fup = lookup(id, type)) == 0)
228 fup = addid(id, type, (char *)0);
229 fup->fu_dqblk = dqbuf;
232 printf("%*s Block limits File limits\n",
233 max(UT_NAMESIZE,10), " ");
234 printf("%s%*s used soft hard grace used soft hard grace\n",
235 type == USRQUOTA ? "User " : "Group", max(UT_NAMESIZE,10), " ");
236 for (id = 0; id <= highid[type]; id++) {
237 fup = lookup(id, type);
240 if (fup->fu_dqblk.dqb_curinodes == 0 &&
241 fup->fu_dqblk.dqb_curblocks == 0)
243 printf("%-*s ", max(UT_NAMESIZE,10), fup->fu_name);
244 printf("%c%c %8lu %8lu %8lu %6s",
245 fup->fu_dqblk.dqb_bsoftlimit &&
246 fup->fu_dqblk.dqb_curblocks >=
247 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
248 fup->fu_dqblk.dqb_isoftlimit &&
249 fup->fu_dqblk.dqb_curinodes >=
250 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
251 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
252 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
253 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
254 fup->fu_dqblk.dqb_bsoftlimit &&
255 fup->fu_dqblk.dqb_curblocks >=
256 fup->fu_dqblk.dqb_bsoftlimit ?
257 timeprt(fup->fu_dqblk.dqb_btime) : "-");
258 printf(" %7lu %7lu %7lu %6s\n",
259 (u_long)fup->fu_dqblk.dqb_curinodes,
260 (u_long)fup->fu_dqblk.dqb_isoftlimit,
261 (u_long)fup->fu_dqblk.dqb_ihardlimit,
262 fup->fu_dqblk.dqb_isoftlimit &&
263 fup->fu_dqblk.dqb_curinodes >=
264 fup->fu_dqblk.dqb_isoftlimit ?
265 timeprt(fup->fu_dqblk.dqb_itime) : "-");
266 fup->fu_dqblk = zerodqblk;
272 * Check to see if target appears in list of size cnt.
275 oneof(target, list, cnt)
276 register char *target, *list[];
281 for (i = 0; i < cnt; i++)
282 if (strcmp(target, list[i]) == 0)
288 * Check to see if a particular quota is to be enabled.
291 hasquota(fs, type, qfnamep)
299 static char initname, usrname[100], grpname[100];
300 static char buf[BUFSIZ];
303 (void)snprintf(usrname, sizeof(usrname), "%s%s",
304 qfextension[USRQUOTA], qfname);
305 (void)snprintf(grpname, sizeof(grpname), "%s%s",
306 qfextension[GRPQUOTA], qfname);
309 strcpy(buf, fs->fs_mntops);
310 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
311 if ((cp = index(opt, '=')))
313 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
315 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
323 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
324 qfname, qfextension[type]);
327 if (statfs(fs->fs_file, &sfb) != 0) {
328 warn("cannot statfs mount point %s", fs->fs_file);
331 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
332 warnx("%s not mounted for %s quotas", fs->fs_file,
333 type == USRQUOTA ? "user" : "group");
340 * Routines to manage the file usage table.
342 * Lookup an id of a specific type.
349 register struct fileusage *fup;
351 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
352 if (fup->fu_id == id)
354 return ((struct fileusage *)0);
358 * Add a new file usage id if it does not already exist.
361 addid(id, type, name)
366 struct fileusage *fup, **fhp;
369 if ((fup = lookup(id, type)))
375 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
376 errx(1, "out of memory for fileusage structures");
377 fhp = &fuhead[type][id & (FUHASH - 1)];
381 if (id > highid[type])
384 bcopy(name, fup->fu_name, len + 1);
386 sprintf(fup->fu_name, "%lu", id);
392 * Calculate the grace period and return a printable string for it.
398 time_t hours, minutes;
405 strlcpy(buf, "none", sizeof (buf));
409 minutes = (seconds + 30) / 60;
410 hours = (minutes + 30) / 60;
412 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
416 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
420 sprintf(buf, "%2ld", (long)minutes);