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. 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
33 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
43 __RCSID("$NetBSD: repquota.c,v 1.23 2008/07/21 13:36:59 lukem Exp $");
50 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <ufs/ufs/quota.h>
63 const char *qfname
= QUOTAFILENAME
;
64 const char *qfextension
[] = INITQFNAMES
;
67 struct fileusage
*fu_next
;
68 struct dqblk fu_dqblk
;
73 #define FUHASH 1024 /* must be power of two */
74 struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
75 u_long highid
[MAXQUOTAS
]; /* highest addid()'ed identifier per type */
77 int vflag
; /* verbose */
78 int aflag
; /* all file systems */
80 struct fileusage
*addid
__P((u_long
, int, const char *));
81 int hasquota
__P((struct fstab
*, int, char **));
82 struct fileusage
*lookup
__P((u_long
, int));
83 int main
__P((int, char **));
84 int oneof
__P((const char *, char **, int));
85 int repquota
__P((struct fstab
*, int, char *));
86 const char *timeprt
__P((time_t));
87 void usage
__P((void));
97 int gflag
= 0, uflag
= 0, errs
= 0;
98 long i
, argnum
, done
= 0;
102 while ((ch
= getopt(argc
, argv
, "aguv")) != -1) {
122 if (argc
== 0 && !aflag
)
124 if (!gflag
&& !uflag
) {
131 while ((gr
= getgrent()) != 0)
132 (void) addid((u_long
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
137 while ((pw
= getpwent()) != 0)
138 (void) addid((u_long
)pw
->pw_uid
, USRQUOTA
, pw
->pw_name
);
142 while ((fs
= getfsent()) != NULL
) {
143 if (strcmp(fs
->fs_vfstype
, "ffs"))
146 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
147 errs
+= repquota(fs
, GRPQUOTA
, qfnp
);
148 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
149 errs
+= repquota(fs
, USRQUOTA
, qfnp
);
152 if ((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
153 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) {
155 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
156 errs
+= repquota(fs
, GRPQUOTA
, qfnp
);
157 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
158 errs
+= repquota(fs
, USRQUOTA
, qfnp
);
162 for (i
= 0; i
< argc
; i
++)
163 if ((done
& (1 << i
)) == 0)
164 fprintf(stderr
, "%s not found in fstab\n", argv
[i
]);
171 fprintf(stderr
, "usage:\n\t%s\n\t%s\n",
172 "repquota [-v] [-g] [-u] -a",
173 "repquota [-v] [-g] [-u] filesys ...");
178 repquota(fs
, type
, qfpathname
)
183 struct fileusage
*fup
;
187 static struct dqblk zerodqblk
;
188 static int warned
= 0;
189 static int multiple
= 0;
191 if (quotactl(fs
->fs_file
, QCMD(Q_SYNC
, type
), 0, 0) < 0 &&
192 errno
== EOPNOTSUPP
&& !warned
&& vflag
) {
195 "*** Warning: Quotas are not compiled into this kernel\n");
200 fprintf(stdout
, "*** Report for %s quotas on %s (%s)\n",
201 qfextension
[type
], fs
->fs_file
, fs
->fs_spec
);
202 if ((qf
= fopen(qfpathname
, "r")) == NULL
) {
206 for (id
= 0; ; id
++) {
207 fread(&dqbuf
, sizeof(struct dqblk
), 1, qf
);
210 if (dqbuf
.dqb_curinodes
== 0 && dqbuf
.dqb_curblocks
== 0)
212 if ((fup
= lookup(id
, type
)) == 0)
213 fup
= addid(id
, type
, (char *)0);
214 fup
->fu_dqblk
= dqbuf
;
217 printf(" Block limits File limits\n");
218 printf(type
== USRQUOTA
? "User " : "Group");
219 printf(" used soft hard grace used soft hard grace\n");
220 for (id
= 0; id
<= highid
[type
]; id
++) {
221 fup
= lookup(id
, type
);
224 if (fup
->fu_dqblk
.dqb_curinodes
== 0 &&
225 fup
->fu_dqblk
.dqb_curblocks
== 0)
227 if (strlen(fup
->fu_name
) > 9)
228 printf("%s ", fup
->fu_name
);
230 printf("%-10s", fup
->fu_name
);
231 printf("%c%c%8d%8d%8d%7s",
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 (int)(dbtob((u_quad_t
)fup
->fu_dqblk
.dqb_curblocks
) / 1024),
239 (int)(dbtob((u_quad_t
)fup
->fu_dqblk
.dqb_bsoftlimit
) / 1024),
240 (int)(dbtob((u_quad_t
)fup
->fu_dqblk
.dqb_bhardlimit
) / 1024),
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(" %8d%8d%8d%7s\n",
246 fup
->fu_dqblk
.dqb_curinodes
,
247 fup
->fu_dqblk
.dqb_isoftlimit
,
248 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(target
, list
, cnt
)
269 for (i
= 0; i
< cnt
; i
++)
270 if (strcmp(target
, list
[i
]) == 0)
276 * Check to see if a particular quota is to be enabled.
279 hasquota(fs
, type
, qfnamep
)
286 static char initname
, usrname
[100], grpname
[100];
287 static char buf
[BUFSIZ
];
290 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
291 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
294 strcpy(buf
, fs
->fs_mntops
);
295 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
296 if ((cp
= strchr(opt
, '=')) != NULL
)
298 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
300 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
309 (void) sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
315 * Routines to manage the file usage table.
317 * Lookup an id of a specific type.
324 struct fileusage
*fup
;
326 for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= 0; fup
= fup
->fu_next
)
327 if (fup
->fu_id
== id
)
329 return ((struct fileusage
*)0);
333 * Add a new file usage id if it does not already exist.
336 addid(id
, type
, name
)
341 struct fileusage
*fup
, **fhp
;
344 if ((fup
= lookup(id
, type
)) != NULL
)
350 if ((fup
= (struct fileusage
*)calloc(1, sizeof(*fup
) + len
)) == NULL
) {
351 fprintf(stderr
, "out of memory for fileusage structures\n");
354 fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
358 if (id
> highid
[type
])
361 memmove(fup
->fu_name
, name
, len
+ 1);
363 sprintf(fup
->fu_name
, "%lu", (u_long
)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", (long)((hours
+ 12) / 24));
391 sprintf(buf
, "%2ld:%ld", (long)(minutes
/ 60),
392 (long)(minutes
% 60));
395 sprintf(buf
, "%2ld", (long)minutes
);