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>
53 #include <ufs/ufs/quota.h>
69 /* Let's be paranoid about block size */
72 ((off_t)(db) >> (10-DEV_BSHIFT))
75 ((off_t)(db) << (DEV_BSHIFT-10))
77 #define dbtokb(db) (db)
80 #define max(a,b) ((a) >= (b) ? (a) : (b))
82 static const char *qfextension[] = INITQFNAMES;
85 struct fileusage *fu_next;
90 #define FUHASH 1024 /* must be power of two */
91 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
92 static struct fileusage *lookup(u_long, int);
93 static struct fileusage *addid(u_long, int, char *);
94 static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
96 static int vflag; /* verbose */
97 static int aflag; /* all filesystems */
98 static int nflag; /* display user/group by id */
99 static int hflag; /* display in human readable format */
101 int oneof(char *, char *[], int);
102 int repquota(struct fstab *, int);
103 char *timeprt(time_t);
104 static void prthumanval(int64_t bytes);
105 static void usage(void);
108 main(int argc, char *argv[])
113 int ch, gflag = 0, uflag = 0, errs = 0;
114 long i, argnum, done = 0;
116 while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
142 if (argc == 0 && !aflag)
144 if (!gflag && !uflag) {
149 if (gflag && !nflag) {
151 while ((gr = getgrent()) != 0)
152 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
155 if (uflag && !nflag) {
157 while ((pw = getpwent()) != 0)
158 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
162 while ((fs = getfsent()) != NULL) {
163 if (strcmp(fs->fs_vfstype, "ufs"))
167 errs += repquota(fs, GRPQUOTA);
169 errs += repquota(fs, USRQUOTA);
172 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
173 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
176 errs += repquota(fs, GRPQUOTA);
178 errs += repquota(fs, USRQUOTA);
182 for (i = 0; i < argc; i++)
183 if ((done & (1 << i)) == 0)
184 warnx("%s not found in fstab", argv[i]);
191 fprintf(stderr, "%s\n%s\n",
192 "usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
193 " repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
198 repquota(struct fstab *fs, int type)
200 struct fileusage *fup;
201 struct quotafile *qf;
204 static int multiple = 0;
206 if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
207 if (vflag && !aflag) {
210 fprintf(stdout, "*** No %s quotas on %s (%s)\n",
211 qfextension[type], fs->fs_file, fs->fs_spec);
219 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
220 qfextension[type], fs->fs_file, fs->fs_spec);
221 printf("%*s Block limits File limits\n",
222 max(MAXLOGNAME - 1, 10), " ");
223 printf("User%*s used soft hard grace used soft hard grace\n",
224 max(MAXLOGNAME - 1, 10), " ");
225 maxid = quota_maxid(qf);
226 for (id = 0; id <= maxid; id++) {
227 if (quota_read(qf, &dqbuf, id) != 0)
229 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
231 if ((fup = lookup(id, type)) == 0)
232 fup = addid(id, type, (char *)0);
233 printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
235 dqbuf.dqb_bsoftlimit &&
236 dqbuf.dqb_curblocks >=
237 dqbuf.dqb_bsoftlimit ? '+' : '-',
238 dqbuf.dqb_isoftlimit &&
239 dqbuf.dqb_curinodes >=
240 dqbuf.dqb_isoftlimit ? '+' : '-');
241 prthumanval(dqbuf.dqb_curblocks);
242 prthumanval(dqbuf.dqb_bsoftlimit);
243 prthumanval(dqbuf.dqb_bhardlimit);
245 dqbuf.dqb_bsoftlimit &&
246 dqbuf.dqb_curblocks >=
247 dqbuf.dqb_bsoftlimit ?
248 timeprt(dqbuf.dqb_btime) : "-");
249 printf(" %7ju %7ju %7ju %6s\n",
250 (uintmax_t)dqbuf.dqb_curinodes,
251 (uintmax_t)dqbuf.dqb_isoftlimit,
252 (uintmax_t)dqbuf.dqb_ihardlimit,
253 dqbuf.dqb_isoftlimit &&
254 dqbuf.dqb_curinodes >=
255 dqbuf.dqb_isoftlimit ?
256 timeprt(dqbuf.dqb_itime) : "-");
263 prthumanval(int64_t blocks)
269 printf(" %6ju", (uintmax_t)dbtokb(blocks));
272 flags = HN_NOSPACE | HN_DECIMAL;
275 humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
276 dbtob(blocks), "", HN_AUTOSCALE, flags);
277 (void)printf("%7s", buf);
281 * Check to see if target appears in list of size cnt.
284 oneof(char *target, char *list[], int cnt)
288 for (i = 0; i < cnt; i++)
289 if (strcmp(target, list[i]) == 0)
295 * Routines to manage the file usage table.
297 * Lookup an id of a specific type.
300 lookup(u_long id, int type)
302 struct fileusage *fup;
304 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
305 if (fup->fu_id == id)
307 return ((struct fileusage *)0);
311 * Add a new file usage id if it does not already exist.
314 addid(u_long id, int type, char *name)
316 struct fileusage *fup, **fhp;
319 if ((fup = lookup(id, type)))
325 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
326 errx(1, "out of memory for fileusage structures");
327 fhp = &fuhead[type][id & (FUHASH - 1)];
331 if (id > highid[type])
334 bcopy(name, fup->fu_name, len + 1);
336 sprintf(fup->fu_name, "%lu", id);
342 * Calculate the grace period and return a printable string for it.
345 timeprt(time_t seconds)
347 time_t hours, minutes;
354 strlcpy(buf, "none", sizeof (buf));
358 minutes = (seconds + 30) / 60;
359 hours = (minutes + 30) / 60;
361 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
365 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
369 sprintf(buf, "%2ld", (long)minutes);