repquota.c revision 1.15 1 1.1 cgd /*
2 1.4 mycroft * Copyright (c) 1980, 1990, 1993
3 1.4 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Robert Elz at The University of Melbourne.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.10 mrg #include <sys/cdefs.h>
38 1.1 cgd #ifndef lint
39 1.10 mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
40 1.10 mrg The Regents of the University of California. All rights reserved.\n");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.10 mrg #if 0
45 1.10 mrg static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
46 1.10 mrg #else
47 1.15 ad __RCSID("$NetBSD: repquota.c,v 1.15 1999/10/06 12:17:31 ad Exp $");
48 1.10 mrg #endif
49 1.1 cgd #endif /* not lint */
50 1.1 cgd
51 1.1 cgd /*
52 1.1 cgd * Quota report
53 1.1 cgd */
54 1.1 cgd #include <sys/param.h>
55 1.1 cgd #include <sys/stat.h>
56 1.10 mrg #include <sys/queue.h>
57 1.3 cgd #include <ufs/ufs/quota.h>
58 1.11 lukem #include <errno.h>
59 1.1 cgd #include <fstab.h>
60 1.11 lukem #include <grp.h>
61 1.1 cgd #include <pwd.h>
62 1.1 cgd #include <stdio.h>
63 1.11 lukem #include <stdlib.h>
64 1.6 cgd #include <string.h>
65 1.11 lukem #include <unistd.h>
66 1.1 cgd
67 1.1 cgd char *qfname = QUOTAFILENAME;
68 1.1 cgd char *qfextension[] = INITQFNAMES;
69 1.1 cgd
70 1.1 cgd struct fileusage {
71 1.1 cgd struct fileusage *fu_next;
72 1.1 cgd struct dqblk fu_dqblk;
73 1.1 cgd u_long fu_id;
74 1.1 cgd char fu_name[1];
75 1.1 cgd /* actually bigger */
76 1.1 cgd };
77 1.1 cgd #define FUHASH 1024 /* must be power of two */
78 1.1 cgd struct fileusage *fuhead[MAXQUOTAS][FUHASH];
79 1.1 cgd u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
80 1.1 cgd
81 1.1 cgd int vflag; /* verbose */
82 1.1 cgd int aflag; /* all file systems */
83 1.1 cgd
84 1.14 mycroft struct fileusage *addid __P((u_long, int, const char *));
85 1.11 lukem int hasquota __P((struct fstab *, int, char **));
86 1.11 lukem struct fileusage *lookup __P((u_long, int));
87 1.11 lukem int main __P((int, char **));
88 1.14 mycroft int oneof __P((const char *, char **, int));
89 1.11 lukem int repquota __P((struct fstab *, int, char *));
90 1.11 lukem char *timeprt __P((time_t));
91 1.11 lukem void usage __P((void));
92 1.11 lukem
93 1.11 lukem int
94 1.1 cgd main(argc, argv)
95 1.1 cgd int argc;
96 1.1 cgd char **argv;
97 1.1 cgd {
98 1.11 lukem struct fstab *fs;
99 1.11 lukem struct passwd *pw;
100 1.11 lukem struct group *gr;
101 1.1 cgd int gflag = 0, uflag = 0, errs = 0;
102 1.1 cgd long i, argnum, done = 0;
103 1.1 cgd extern char *optarg;
104 1.1 cgd extern int optind;
105 1.9 mark char *qfnp;
106 1.9 mark int ch;
107 1.1 cgd
108 1.9 mark while ((ch = getopt(argc, argv, "aguv")) != -1) {
109 1.1 cgd switch(ch) {
110 1.1 cgd case 'a':
111 1.1 cgd aflag++;
112 1.1 cgd break;
113 1.1 cgd case 'g':
114 1.1 cgd gflag++;
115 1.1 cgd break;
116 1.1 cgd case 'u':
117 1.1 cgd uflag++;
118 1.1 cgd break;
119 1.1 cgd case 'v':
120 1.1 cgd vflag++;
121 1.1 cgd break;
122 1.1 cgd default:
123 1.1 cgd usage();
124 1.1 cgd }
125 1.1 cgd }
126 1.1 cgd argc -= optind;
127 1.1 cgd argv += optind;
128 1.1 cgd if (argc == 0 && !aflag)
129 1.1 cgd usage();
130 1.1 cgd if (!gflag && !uflag) {
131 1.1 cgd if (aflag)
132 1.1 cgd gflag++;
133 1.1 cgd uflag++;
134 1.1 cgd }
135 1.1 cgd if (gflag) {
136 1.1 cgd setgrent();
137 1.1 cgd while ((gr = getgrent()) != 0)
138 1.1 cgd (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
139 1.1 cgd endgrent();
140 1.1 cgd }
141 1.1 cgd if (uflag) {
142 1.1 cgd setpwent();
143 1.1 cgd while ((pw = getpwent()) != 0)
144 1.1 cgd (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
145 1.1 cgd endpwent();
146 1.1 cgd }
147 1.1 cgd setfsent();
148 1.1 cgd while ((fs = getfsent()) != NULL) {
149 1.8 jtc if (strcmp(fs->fs_vfstype, "ffs"))
150 1.1 cgd continue;
151 1.1 cgd if (aflag) {
152 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
153 1.1 cgd errs += repquota(fs, GRPQUOTA, qfnp);
154 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp))
155 1.1 cgd errs += repquota(fs, USRQUOTA, qfnp);
156 1.1 cgd continue;
157 1.1 cgd }
158 1.1 cgd if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
159 1.1 cgd (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
160 1.1 cgd done |= 1 << argnum;
161 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
162 1.1 cgd errs += repquota(fs, GRPQUOTA, qfnp);
163 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp))
164 1.1 cgd errs += repquota(fs, USRQUOTA, qfnp);
165 1.1 cgd }
166 1.1 cgd }
167 1.1 cgd endfsent();
168 1.1 cgd for (i = 0; i < argc; i++)
169 1.1 cgd if ((done & (1 << i)) == 0)
170 1.1 cgd fprintf(stderr, "%s not found in fstab\n", argv[i]);
171 1.1 cgd exit(errs);
172 1.1 cgd }
173 1.1 cgd
174 1.11 lukem void
175 1.1 cgd usage()
176 1.1 cgd {
177 1.1 cgd fprintf(stderr, "Usage:\n\t%s\n\t%s\n",
178 1.1 cgd "repquota [-v] [-g] [-u] -a",
179 1.1 cgd "repquota [-v] [-g] [-u] filesys ...");
180 1.1 cgd exit(1);
181 1.1 cgd }
182 1.1 cgd
183 1.11 lukem int
184 1.1 cgd repquota(fs, type, qfpathname)
185 1.11 lukem struct fstab *fs;
186 1.1 cgd int type;
187 1.1 cgd char *qfpathname;
188 1.1 cgd {
189 1.11 lukem struct fileusage *fup;
190 1.1 cgd FILE *qf;
191 1.1 cgd u_long id;
192 1.1 cgd struct dqblk dqbuf;
193 1.1 cgd static struct dqblk zerodqblk;
194 1.1 cgd static int warned = 0;
195 1.1 cgd static int multiple = 0;
196 1.1 cgd extern int errno;
197 1.1 cgd
198 1.1 cgd if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
199 1.1 cgd errno == EOPNOTSUPP && !warned && vflag) {
200 1.1 cgd warned++;
201 1.1 cgd fprintf(stdout,
202 1.1 cgd "*** Warning: Quotas are not compiled into this kernel\n");
203 1.1 cgd }
204 1.1 cgd if (multiple++)
205 1.1 cgd printf("\n");
206 1.1 cgd if (vflag)
207 1.1 cgd fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
208 1.1 cgd qfextension[type], fs->fs_file, fs->fs_spec);
209 1.1 cgd if ((qf = fopen(qfpathname, "r")) == NULL) {
210 1.1 cgd perror(qfpathname);
211 1.1 cgd return (1);
212 1.1 cgd }
213 1.1 cgd for (id = 0; ; id++) {
214 1.1 cgd fread(&dqbuf, sizeof(struct dqblk), 1, qf);
215 1.1 cgd if (feof(qf))
216 1.1 cgd break;
217 1.1 cgd if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
218 1.1 cgd continue;
219 1.1 cgd if ((fup = lookup(id, type)) == 0)
220 1.1 cgd fup = addid(id, type, (char *)0);
221 1.1 cgd fup->fu_dqblk = dqbuf;
222 1.1 cgd }
223 1.1 cgd fclose(qf);
224 1.1 cgd printf(" Block limits File limits\n");
225 1.15 ad printf(type == USRQUOTA ? "User " : "Group");
226 1.15 ad printf(" used soft hard grace used soft hard grace\n");
227 1.1 cgd for (id = 0; id <= highid[type]; id++) {
228 1.1 cgd fup = lookup(id, type);
229 1.1 cgd if (fup == 0)
230 1.1 cgd continue;
231 1.1 cgd if (fup->fu_dqblk.dqb_curinodes == 0 &&
232 1.1 cgd fup->fu_dqblk.dqb_curblocks == 0)
233 1.1 cgd continue;
234 1.1 cgd printf("%-10s", fup->fu_name);
235 1.1 cgd printf("%c%c%8d%8d%8d%7s",
236 1.1 cgd fup->fu_dqblk.dqb_bsoftlimit &&
237 1.1 cgd fup->fu_dqblk.dqb_curblocks >=
238 1.1 cgd fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
239 1.1 cgd fup->fu_dqblk.dqb_isoftlimit &&
240 1.1 cgd fup->fu_dqblk.dqb_curinodes >=
241 1.1 cgd fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
242 1.1 cgd dbtob(fup->fu_dqblk.dqb_curblocks) / 1024,
243 1.1 cgd dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024,
244 1.1 cgd dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024,
245 1.1 cgd fup->fu_dqblk.dqb_bsoftlimit &&
246 1.1 cgd fup->fu_dqblk.dqb_curblocks >=
247 1.1 cgd fup->fu_dqblk.dqb_bsoftlimit ?
248 1.1 cgd timeprt(fup->fu_dqblk.dqb_btime) : "");
249 1.1 cgd printf(" %6d%6d%6d%7s\n",
250 1.1 cgd fup->fu_dqblk.dqb_curinodes,
251 1.1 cgd fup->fu_dqblk.dqb_isoftlimit,
252 1.1 cgd fup->fu_dqblk.dqb_ihardlimit,
253 1.1 cgd fup->fu_dqblk.dqb_isoftlimit &&
254 1.1 cgd fup->fu_dqblk.dqb_curinodes >=
255 1.1 cgd fup->fu_dqblk.dqb_isoftlimit ?
256 1.1 cgd timeprt(fup->fu_dqblk.dqb_itime) : "");
257 1.1 cgd fup->fu_dqblk = zerodqblk;
258 1.1 cgd }
259 1.1 cgd return (0);
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd /*
263 1.1 cgd * Check to see if target appears in list of size cnt.
264 1.1 cgd */
265 1.11 lukem int
266 1.1 cgd oneof(target, list, cnt)
267 1.14 mycroft const char *target;
268 1.14 mycroft char *list[];
269 1.1 cgd int cnt;
270 1.1 cgd {
271 1.11 lukem int i;
272 1.1 cgd
273 1.1 cgd for (i = 0; i < cnt; i++)
274 1.1 cgd if (strcmp(target, list[i]) == 0)
275 1.1 cgd return (i);
276 1.1 cgd return (-1);
277 1.1 cgd }
278 1.1 cgd
279 1.1 cgd /*
280 1.1 cgd * Check to see if a particular quota is to be enabled.
281 1.1 cgd */
282 1.11 lukem int
283 1.1 cgd hasquota(fs, type, qfnamep)
284 1.11 lukem struct fstab *fs;
285 1.1 cgd int type;
286 1.1 cgd char **qfnamep;
287 1.1 cgd {
288 1.11 lukem char *opt;
289 1.13 fair char *cp = NULL;
290 1.1 cgd static char initname, usrname[100], grpname[100];
291 1.1 cgd static char buf[BUFSIZ];
292 1.1 cgd
293 1.1 cgd if (!initname) {
294 1.1 cgd sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
295 1.1 cgd sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
296 1.1 cgd initname = 1;
297 1.1 cgd }
298 1.1 cgd strcpy(buf, fs->fs_mntops);
299 1.1 cgd for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
300 1.11 lukem if ((cp = strchr(opt, '=')) != NULL)
301 1.1 cgd *cp++ = '\0';
302 1.1 cgd if (type == USRQUOTA && strcmp(opt, usrname) == 0)
303 1.1 cgd break;
304 1.1 cgd if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
305 1.1 cgd break;
306 1.1 cgd }
307 1.1 cgd if (!opt)
308 1.1 cgd return (0);
309 1.1 cgd if (cp) {
310 1.1 cgd *qfnamep = cp;
311 1.1 cgd return (1);
312 1.1 cgd }
313 1.1 cgd (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
314 1.1 cgd *qfnamep = buf;
315 1.1 cgd return (1);
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd /*
319 1.1 cgd * Routines to manage the file usage table.
320 1.1 cgd *
321 1.1 cgd * Lookup an id of a specific type.
322 1.1 cgd */
323 1.1 cgd struct fileusage *
324 1.1 cgd lookup(id, type)
325 1.1 cgd u_long id;
326 1.1 cgd int type;
327 1.1 cgd {
328 1.11 lukem struct fileusage *fup;
329 1.1 cgd
330 1.1 cgd for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
331 1.1 cgd if (fup->fu_id == id)
332 1.1 cgd return (fup);
333 1.1 cgd return ((struct fileusage *)0);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * Add a new file usage id if it does not already exist.
338 1.1 cgd */
339 1.1 cgd struct fileusage *
340 1.1 cgd addid(id, type, name)
341 1.1 cgd u_long id;
342 1.1 cgd int type;
343 1.14 mycroft const char *name;
344 1.1 cgd {
345 1.1 cgd struct fileusage *fup, **fhp;
346 1.1 cgd int len;
347 1.1 cgd
348 1.11 lukem if ((fup = lookup(id, type)) != NULL)
349 1.1 cgd return (fup);
350 1.1 cgd if (name)
351 1.1 cgd len = strlen(name);
352 1.1 cgd else
353 1.1 cgd len = 10;
354 1.1 cgd if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) {
355 1.1 cgd fprintf(stderr, "out of memory for fileusage structures\n");
356 1.1 cgd exit(1);
357 1.1 cgd }
358 1.1 cgd fhp = &fuhead[type][id & (FUHASH - 1)];
359 1.1 cgd fup->fu_next = *fhp;
360 1.1 cgd *fhp = fup;
361 1.1 cgd fup->fu_id = id;
362 1.1 cgd if (id > highid[type])
363 1.1 cgd highid[type] = id;
364 1.1 cgd if (name) {
365 1.12 lukem memmove(fup->fu_name, name, len + 1);
366 1.1 cgd } else {
367 1.11 lukem sprintf(fup->fu_name, "%lu", (u_long)id);
368 1.1 cgd }
369 1.1 cgd return (fup);
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd /*
373 1.1 cgd * Calculate the grace period and return a printable string for it.
374 1.1 cgd */
375 1.1 cgd char *
376 1.1 cgd timeprt(seconds)
377 1.1 cgd time_t seconds;
378 1.1 cgd {
379 1.1 cgd time_t hours, minutes;
380 1.1 cgd static char buf[20];
381 1.1 cgd static time_t now;
382 1.1 cgd
383 1.1 cgd if (now == 0)
384 1.1 cgd time(&now);
385 1.1 cgd if (now > seconds)
386 1.1 cgd return ("none");
387 1.1 cgd seconds -= now;
388 1.1 cgd minutes = (seconds + 30) / 60;
389 1.1 cgd hours = (minutes + 30) / 60;
390 1.1 cgd if (hours >= 36) {
391 1.11 lukem sprintf(buf, "%lddays", (long)((hours + 12) / 24));
392 1.1 cgd return (buf);
393 1.1 cgd }
394 1.1 cgd if (minutes >= 60) {
395 1.11 lukem sprintf(buf, "%2ld:%ld", (long)(minutes / 60),
396 1.11 lukem (long)(minutes % 60));
397 1.1 cgd return (buf);
398 1.1 cgd }
399 1.11 lukem sprintf(buf, "%2ld", (long)minutes);
400 1.1 cgd return (buf);
401 1.1 cgd }
402