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
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)repquota.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.sbin/repquota/repquota.c,v 1.9.2.2 2002/03/15 22:18:25 mikeh Exp $
39 * $DragonFly: src/usr.sbin/repquota/repquota.c,v 1.7 2006/04/03 01:58:49 dillon Exp $
45 #include <sys/param.h>
47 #include <vfs/ufs/quota.h>
59 /* Let's be paranoid about block size */
62 ((off_t)(db) >> (10-DEV_BSHIFT))
65 ((off_t)(db) << (DEV_BSHIFT-10))
67 #define dbtokb(db) (db)
70 #define max(a,b) ((a) >= (b) ? (a) : (b))
72 const char *qfname
= QUOTAFILENAME
;
73 const char *qfextension
[] = INITQFNAMES
;
76 struct fileusage
*fu_next
;
77 struct ufs_dqblk fu_dqblk
;
82 #define FUHASH 1024 /* must be power of two */
83 struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
84 struct fileusage
*lookup(u_long
, int);
85 struct fileusage
*addid(u_long
, int, char *);
86 u_long highid
[MAXQUOTAS
]; /* highest addid()'ed identifier per type */
88 int vflag
; /* verbose */
89 int aflag
; /* all file systems */
91 int hasquota(struct fstab
*, int, char **);
92 int oneof(char *, char *[], int);
93 int repquota(struct fstab
*, int, char *);
94 char *timeprt(time_t);
95 static void usage(void);
98 main(int argc
, char **argv
)
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 addid((u_long
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
142 while ((pw
= getpwent()) != 0)
143 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(struct fstab
*fs
, int type
, char *qfpathname
)
185 struct fileusage
*fup
;
188 struct ufs_dqblk dqbuf
;
189 static struct ufs_dqblk zerodqblk
;
190 static int warned
= 0;
191 static int multiple
= 0;
193 if (quotactl(fs
->fs_file
, QCMD(Q_SYNC
, type
), 0, 0) < 0 &&
194 errno
== EOPNOTSUPP
&& !warned
&& vflag
) {
197 "*** Warning: Quotas are not compiled into this kernel\n");
202 fprintf(stdout
, "*** Report for %s quotas on %s (%s)\n",
203 qfextension
[type
], fs
->fs_file
, fs
->fs_spec
);
204 if ((qf
= fopen(qfpathname
, "r")) == NULL
) {
205 warn("%s", qfpathname
);
208 for (id
= 0; ; id
++) {
209 fread(&dqbuf
, sizeof(struct ufs_dqblk
), 1, qf
);
212 if (dqbuf
.dqb_curinodes
== 0 && dqbuf
.dqb_curblocks
== 0)
214 if ((fup
= lookup(id
, type
)) == 0)
215 fup
= addid(id
, type
, (char *)0);
216 fup
->fu_dqblk
= dqbuf
;
219 printf("%*s Block limits File limits\n",
220 max(UT_NAMESIZE
,10), " ");
221 printf("User%*s used soft hard grace used soft hard grace\n",
222 max(UT_NAMESIZE
,10), " ");
223 for (id
= 0; id
<= highid
[type
]; id
++) {
224 fup
= lookup(id
, type
);
227 if (fup
->fu_dqblk
.dqb_curinodes
== 0 &&
228 fup
->fu_dqblk
.dqb_curblocks
== 0)
230 printf("%-*s", max(UT_NAMESIZE
,10), fup
->fu_name
);
231 printf("%c%c %8lu %8lu %8lu %6s",
232 fup
->fu_dqblk
.dqb_bsoftlimit
&&
233 fup
->fu_dqblk
.dqb_curblocks
>=
234 fup
->fu_dqblk
.dqb_bsoftlimit
? '+' : '-',
235 fup
->fu_dqblk
.dqb_isoftlimit
&&
236 fup
->fu_dqblk
.dqb_curinodes
>=
237 fup
->fu_dqblk
.dqb_isoftlimit
? '+' : '-',
238 (u_long
)(dbtokb(fup
->fu_dqblk
.dqb_curblocks
)),
239 (u_long
)(dbtokb(fup
->fu_dqblk
.dqb_bsoftlimit
)),
240 (u_long
)(dbtokb(fup
->fu_dqblk
.dqb_bhardlimit
)),
241 fup
->fu_dqblk
.dqb_bsoftlimit
&&
242 fup
->fu_dqblk
.dqb_curblocks
>=
243 fup
->fu_dqblk
.dqb_bsoftlimit
?
244 timeprt(fup
->fu_dqblk
.dqb_btime
) : "-");
245 printf(" %7lu %7lu %7lu %6s\n",
246 (u_long
)fup
->fu_dqblk
.dqb_curinodes
,
247 (u_long
)fup
->fu_dqblk
.dqb_isoftlimit
,
248 (u_long
)fup
->fu_dqblk
.dqb_ihardlimit
,
249 fup
->fu_dqblk
.dqb_isoftlimit
&&
250 fup
->fu_dqblk
.dqb_curinodes
>=
251 fup
->fu_dqblk
.dqb_isoftlimit
?
252 timeprt(fup
->fu_dqblk
.dqb_itime
) : "-");
253 fup
->fu_dqblk
= zerodqblk
;
259 * Check to see if target appears in list of size cnt.
262 oneof(char *target
, char *list
[], int cnt
)
266 for (i
= 0; i
< cnt
; i
++)
267 if (strcmp(target
, list
[i
]) == 0)
273 * Check to see if a particular quota is to be enabled.
276 hasquota(struct fstab
*fs
, int type
, char **qfnamep
)
280 static char initname
, usrname
[100], grpname
[100];
281 static char buf
[BUFSIZ
];
284 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
285 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
288 strcpy(buf
, fs
->fs_mntops
);
289 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
290 if ((cp
= strchr(opt
, '=')))
292 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
294 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
303 sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
309 * Routines to manage the file usage table.
311 * Lookup an id of a specific type.
314 lookup(u_long id
, int type
)
316 struct fileusage
*fup
;
318 for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= 0; fup
= fup
->fu_next
)
319 if (fup
->fu_id
== id
)
321 return ((struct fileusage
*)0);
325 * Add a new file usage id if it does not already exist.
328 addid(u_long id
, int type
, char *name
)
330 struct fileusage
*fup
, **fhp
;
333 if ((fup
= lookup(id
, type
)))
339 if ((fup
= (struct fileusage
*)calloc(1, sizeof(*fup
) + len
)) == NULL
)
340 errx(1, "out of memory for fileusage structures");
341 fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
345 if (id
> highid
[type
])
348 bcopy(name
, fup
->fu_name
, len
+ 1);
350 sprintf(fup
->fu_name
, "%lu", id
);
356 * Calculate the grace period and return a printable string for it.
359 timeprt(time_t seconds
)
361 time_t hours
, minutes
;
368 strlcpy(buf
, "none", sizeof (buf
));
372 minutes
= (seconds
+ 30) / 60;
373 hours
= (minutes
+ 30) / 60;
375 sprintf(buf
, "%lddays", (long)(hours
+ 12) / 24);
379 sprintf(buf
, "%2ld:%ld", (long)minutes
/ 60,
383 sprintf(buf
, "%2ld", (long)minutes
);