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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
54 #include <sys/param.h>
56 #include <ufs/ufs/quota.h>
68 #define max(a,b) ((a) >= (b) ? (a) : (b))
70 char *qfname = QUOTAFILENAME;
71 char *qfextension[] = INITQFNAMES;
74 struct fileusage *fu_next;
75 struct dqblk fu_dqblk;
80 #define FUHASH 1024 /* must be power of two */
81 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
82 struct fileusage *lookup();
83 struct fileusage *addid();
84 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
86 int vflag; /* verbose */
87 int aflag; /* all file systems */
89 int hasquota __P((struct fstab *, int, char **));
90 int oneof __P((char *, char *[], int));
91 int repquota __P((struct fstab *, int, char *));
92 char *timeprt __P((time_t));
93 static void usage __P((void));
100 register struct fstab *fs;
101 register struct passwd *pw;
102 register struct group *gr;
103 int gflag = 0, uflag = 0, errs = 0;
104 long i, argnum, done = 0;
107 while ((ch = getopt(argc, argv, "aguv")) != -1) {
127 if (argc == 0 && !aflag)
129 if (!gflag && !uflag) {
136 while ((gr = getgrent()) != 0)
137 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
142 while ((pw = getpwent()) != 0)
143 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
147 while ((fs = getfsent()) != NULL) {
148 if (strcmp(fs->fs_vfstype, "ufs"))
151 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
152 errs += repquota(fs, GRPQUOTA, qfnp);
153 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
154 errs += repquota(fs, USRQUOTA, qfnp);
157 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
158 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
160 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
161 errs += repquota(fs, GRPQUOTA, qfnp);
162 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
163 errs += repquota(fs, USRQUOTA, qfnp);
167 for (i = 0; i < argc; i++)
168 if ((done & (1 << i)) == 0)
169 warnx("%s not found in fstab", argv[i]);
176 fprintf(stderr, "%s\n%s\n",
177 "usage: repquota [-v] [-g] [-u] -a",
178 " repquota [-v] [-g] [-u] filesystem ...");
183 repquota(fs, type, qfpathname)
184 register struct fstab *fs;
188 register struct fileusage *fup;
192 static struct dqblk zerodqblk;
193 static int warned = 0;
194 static int multiple = 0;
196 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
197 errno == EOPNOTSUPP && !warned && vflag) {
200 "*** Warning: Quotas are not compiled into this kernel\n");
205 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
206 qfextension[type], fs->fs_file, fs->fs_spec);
207 if ((qf = fopen(qfpathname, "r")) == NULL) {
208 warn("%s", qfpathname);
211 for (id = 0; ; id++) {
212 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
215 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
217 if ((fup = lookup(id, type)) == 0)
218 fup = addid(id, type, (char *)0);
219 fup->fu_dqblk = dqbuf;
222 printf("%*s Block limits File limits\n",
223 max(UT_NAMESIZE,10), " ");
224 printf("User%*s used soft hard grace used soft hard grace\n",
225 max(UT_NAMESIZE,10), " ");
226 for (id = 0; id <= highid[type]; id++) {
227 fup = lookup(id, type);
230 if (fup->fu_dqblk.dqb_curinodes == 0 &&
231 fup->fu_dqblk.dqb_curblocks == 0)
233 printf("%-*s", max(UT_NAMESIZE,10), fup->fu_name);
234 printf("%c%c %8lu %8lu %8lu %6s",
235 fup->fu_dqblk.dqb_bsoftlimit &&
236 fup->fu_dqblk.dqb_curblocks >=
237 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
238 fup->fu_dqblk.dqb_isoftlimit &&
239 fup->fu_dqblk.dqb_curinodes >=
240 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
241 (u_long)(dbtob(fup->fu_dqblk.dqb_curblocks) / 1024),
242 (u_long)(dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024),
243 (u_long)(dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024),
244 fup->fu_dqblk.dqb_bsoftlimit &&
245 fup->fu_dqblk.dqb_curblocks >=
246 fup->fu_dqblk.dqb_bsoftlimit ?
247 timeprt(fup->fu_dqblk.dqb_btime) : "");
248 printf(" %7lu %7lu %7lu %6s\n",
249 fup->fu_dqblk.dqb_curinodes,
250 fup->fu_dqblk.dqb_isoftlimit,
251 fup->fu_dqblk.dqb_ihardlimit,
252 fup->fu_dqblk.dqb_isoftlimit &&
253 fup->fu_dqblk.dqb_curinodes >=
254 fup->fu_dqblk.dqb_isoftlimit ?
255 timeprt(fup->fu_dqblk.dqb_itime) : "");
256 fup->fu_dqblk = zerodqblk;
262 * Check to see if target appears in list of size cnt.
265 oneof(target, list, cnt)
266 register char *target, *list[];
271 for (i = 0; i < cnt; i++)
272 if (strcmp(target, list[i]) == 0)
278 * Check to see if a particular quota is to be enabled.
281 hasquota(fs, type, qfnamep)
282 register struct fstab *fs;
288 static char initname, usrname[100], grpname[100];
289 static char buf[BUFSIZ];
292 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
293 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
296 strcpy(buf, fs->fs_mntops);
297 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
298 if ((cp = index(opt, '=')))
300 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
302 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
311 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
317 * Routines to manage the file usage table.
319 * Lookup an id of a specific type.
326 register struct fileusage *fup;
328 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
329 if (fup->fu_id == id)
331 return ((struct fileusage *)0);
335 * Add a new file usage id if it does not already exist.
338 addid(id, type, name)
343 struct fileusage *fup, **fhp;
346 if ((fup = lookup(id, type)))
352 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
353 errx(1, "out of memory for fileusage structures");
354 fhp = &fuhead[type][id & (FUHASH - 1)];
358 if (id > highid[type])
361 bcopy(name, fup->fu_name, len + 1);
363 sprintf(fup->fu_name, "%lu", id);
369 * Calculate the grace period and return a printable string for it.
375 time_t hours, minutes;
384 minutes = (seconds + 30) / 60;
385 hours = (minutes + 30) / 60;
387 sprintf(buf, "%lddays", (hours + 12) / 24);
391 sprintf(buf, "%2ld:%ld", minutes / 60, minutes % 60);
394 sprintf(buf, "%2ld", minutes);