2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/mount.h>
55 #include <ufs/ufs/quota.h>
71 /* Let's be paranoid about block size */
74 ((off_t)(db) >> (10-DEV_BSHIFT))
77 ((off_t)(db) << (DEV_BSHIFT-10))
79 #define dbtokb(db) (db)
82 #define max(a,b) ((a) >= (b) ? (a) : (b))
84 static const char *qfextension[] = INITQFNAMES;
87 struct fileusage *fu_next;
92 #define FUHASH 1024 /* must be power of two */
93 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
94 static struct fileusage *lookup(u_long, int);
95 static struct fileusage *addid(u_long, int, char *);
96 static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
98 static int vflag; /* verbose */
99 static int aflag; /* all filesystems */
100 static int nflag; /* display user/group by id */
101 static int hflag; /* display in human readable format */
103 int oneof(char *, char *[], int);
104 int repquota(struct fstab *, int);
105 char *timeprt(time_t);
106 static void prthumanval(int64_t bytes);
107 static void usage(void);
110 main(int argc, char *argv[])
115 int ch, gflag = 0, uflag = 0, errs = 0;
116 long i, argnum, done = 0;
118 while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
144 if (argc == 0 && !aflag)
146 if (!gflag && !uflag) {
151 if (gflag && !nflag) {
153 while ((gr = getgrent()) != 0)
154 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
157 if (uflag && !nflag) {
159 while ((pw = getpwent()) != 0)
160 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
164 while ((fs = getfsent()) != NULL) {
165 if (strcmp(fs->fs_vfstype, "ufs"))
169 errs += repquota(fs, GRPQUOTA);
171 errs += repquota(fs, USRQUOTA);
174 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
175 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
178 errs += repquota(fs, GRPQUOTA);
180 errs += repquota(fs, USRQUOTA);
184 for (i = 0; i < argc; i++)
185 if ((done & (1 << i)) == 0)
186 warnx("%s not found in fstab", argv[i]);
193 fprintf(stderr, "%s\n%s\n",
194 "usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
195 " repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
200 repquota(struct fstab *fs, int type)
202 struct fileusage *fup;
203 struct quotafile *qf;
206 static int multiple = 0;
208 if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
209 if (vflag && !aflag) {
212 fprintf(stdout, "*** No %s quotas on %s (%s)\n",
213 qfextension[type], fs->fs_file, fs->fs_spec);
221 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
222 qfextension[type], fs->fs_file, fs->fs_spec);
223 printf("%*s Block limits File limits\n",
224 max(MAXLOGNAME - 1, 10), " ");
225 printf("User%*s used soft hard grace used soft hard grace\n",
226 max(MAXLOGNAME - 1, 10), " ");
227 maxid = quota_maxid(qf);
228 for (id = 0; id <= maxid; id++) {
229 if (quota_read(qf, &dqbuf, id) != 0)
231 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
233 if ((fup = lookup(id, type)) == 0)
234 fup = addid(id, type, (char *)0);
235 printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
237 dqbuf.dqb_bsoftlimit &&
238 dqbuf.dqb_curblocks >=
239 dqbuf.dqb_bsoftlimit ? '+' : '-',
240 dqbuf.dqb_isoftlimit &&
241 dqbuf.dqb_curinodes >=
242 dqbuf.dqb_isoftlimit ? '+' : '-');
243 prthumanval(dqbuf.dqb_curblocks);
244 prthumanval(dqbuf.dqb_bsoftlimit);
245 prthumanval(dqbuf.dqb_bhardlimit);
247 dqbuf.dqb_bsoftlimit &&
248 dqbuf.dqb_curblocks >=
249 dqbuf.dqb_bsoftlimit ?
250 timeprt(dqbuf.dqb_btime) : "-");
251 printf(" %7ju %7ju %7ju %6s\n",
252 (uintmax_t)dqbuf.dqb_curinodes,
253 (uintmax_t)dqbuf.dqb_isoftlimit,
254 (uintmax_t)dqbuf.dqb_ihardlimit,
255 dqbuf.dqb_isoftlimit &&
256 dqbuf.dqb_curinodes >=
257 dqbuf.dqb_isoftlimit ?
258 timeprt(dqbuf.dqb_itime) : "-");
265 prthumanval(int64_t blocks)
271 printf(" %6ju", (uintmax_t)dbtokb(blocks));
274 flags = HN_NOSPACE | HN_DECIMAL;
277 humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
278 dbtob(blocks), "", HN_AUTOSCALE, flags);
279 (void)printf("%7s", buf);
283 * Check to see if target appears in list of size cnt.
286 oneof(char *target, char *list[], int cnt)
290 for (i = 0; i < cnt; i++)
291 if (strcmp(target, list[i]) == 0)
297 * Routines to manage the file usage table.
299 * Lookup an id of a specific type.
302 lookup(u_long id, int type)
304 struct fileusage *fup;
306 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
307 if (fup->fu_id == id)
309 return ((struct fileusage *)0);
313 * Add a new file usage id if it does not already exist.
316 addid(u_long id, int type, char *name)
318 struct fileusage *fup, **fhp;
321 if ((fup = lookup(id, type)))
327 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
328 errx(1, "out of memory for fileusage structures");
329 fhp = &fuhead[type][id & (FUHASH - 1)];
333 if (id > highid[type])
336 bcopy(name, fup->fu_name, len + 1);
338 sprintf(fup->fu_name, "%lu", id);
344 * Calculate the grace period and return a printable string for it.
347 timeprt(time_t seconds)
349 time_t hours, minutes;
356 strlcpy(buf, "none", sizeof (buf));
360 minutes = (seconds + 30) / 60;
361 hours = (minutes + 30) / 60;
363 sprintf(buf, "%lddays", (long)(hours + 12) / 24);
367 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
371 sprintf(buf, "%2ld", (long)minutes);