repquota.c revision 1.28 1 /* $NetBSD: repquota.c,v 1.28 2011/03/06 23:25:42 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
44 #else
45 __RCSID("$NetBSD: repquota.c,v 1.28 2011/03/06 23:25:42 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * Quota report
51 */
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/types.h>
55 #include <sys/statvfs.h>
56 #include <prop/proplib.h>
57 #include <sys/quota.h>
58
59 #include <errno.h>
60 #include <err.h>
61 #include <fstab.h>
62 #include <grp.h>
63 #include <pwd.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include <ufs/ufs/quota2_prop.h>
70 #include <ufs/ufs/quota1.h>
71
72 #include "printquota.h"
73 #include "quotautil.h"
74
75 struct fileusage {
76 struct fileusage *fu_next;
77 struct quota2_entry fu_q2e;
78 uint32_t fu_id;
79 char fu_name[1];
80 /* actually bigger */
81 };
82 #define FUHASH 1024 /* must be power of two */
83 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
84 static uint32_t highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
85 int valid[MAXQUOTAS];
86 static struct quota2_entry defaultq2e[MAXQUOTAS];
87
88 static int vflag = 0; /* verbose */
89 static int aflag = 0; /* all file systems */
90 static int Dflag = 0; /* debug */
91 static int hflag = 0; /* humanize */
92 static int xflag = 0; /* export */
93
94
95 static struct fileusage *addid(uint32_t, int, const char *);
96 static struct fileusage *lookup(uint32_t, int);
97 static struct fileusage *qremove(uint32_t, int);
98 static int repquota(const struct statvfs *, int);
99 static int repquota2(const struct statvfs *, int);
100 static int repquota1(const struct statvfs *, int);
101 static void usage(void) __attribute__((__noreturn__));
102 static void printquotas(int, const struct statvfs *, int);
103 static void exportquotas(void);
104
105 int
106 main(int argc, char **argv)
107 {
108 int gflag = 0, uflag = 0, errs = 0;
109 long i, argnum, done = 0;
110 int ch;
111 struct statvfs *fst;
112 int nfst;
113
114 while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
115 switch(ch) {
116 case 'a':
117 aflag++;
118 break;
119 case 'g':
120 gflag++;
121 break;
122 case 'u':
123 uflag++;
124 break;
125 case 'h':
126 hflag++;
127 break;
128 case 'v':
129 vflag++;
130 break;
131 case 'D':
132 Dflag++;
133 break;
134 case 'x':
135 xflag++;
136 break;
137 default:
138 usage();
139 }
140 }
141 argc -= optind;
142 argv += optind;
143 if (xflag && (argc != 1 || aflag))
144 usage();
145 if (argc == 0 && !aflag)
146 usage();
147 if (!gflag && !uflag) {
148 if (aflag)
149 gflag++;
150 uflag++;
151 }
152
153 nfst = getmntinfo(&fst, MNT_WAIT);
154 if (nfst == 0)
155 errx(1, "no filesystems mounted!");
156 for (i = 0; i < nfst; i++) {
157 if ((fst[i].f_flag & ST_QUOTA) == 0)
158 continue;
159 if (aflag) {
160 if (gflag)
161 errs += repquota(&fst[i], GRPQUOTA);
162 if (uflag)
163 errs += repquota(&fst[i], USRQUOTA);
164 continue;
165 }
166 if ((argnum = oneof(fst[i].f_mntonname, argv, argc)) >= 0 ||
167 (argnum = oneof(fst[i].f_mntfromname, argv, argc)) >= 0) {
168 done |= 1U << argnum;
169 if (gflag)
170 errs += repquota(&fst[i], GRPQUOTA);
171 if (uflag)
172 errs += repquota(&fst[i], USRQUOTA);
173 }
174 }
175 if (xflag)
176 exportquotas();
177 for (i = 0; i < argc; i++)
178 if ((done & (1U << i)) == 0)
179 warnx("%s not mounted", argv[i]);
180 return errs;
181 }
182
183 static void
184 usage(void)
185 {
186 const char *p = getprogname();
187 fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
188 "\t%s [-D] [-v] [-g] [-u] filesys ...\n"
189 "\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
190 exit(1);
191 }
192
193 static int
194 repquota(const struct statvfs *vfs, int type)
195 {
196 if (repquota2(vfs, type) != 0)
197 return repquota1(vfs, type);
198 return 0;
199 }
200
201 static int
202 repquota2(const struct statvfs *vfs, int type)
203 {
204 prop_dictionary_t dict, data, cmd;
205 prop_array_t cmds, datas;
206 struct plistref pref;
207 int8_t error8, version = 0;
208 prop_object_iterator_t cmditer, dataiter;
209 struct quota2_entry *q2ep;
210 struct fileusage *fup;
211 const char *strid;
212 uint32_t id;
213
214 dict = quota2_prop_create();
215 cmds = prop_array_create();
216 datas = prop_array_create();
217
218 if (dict == NULL || cmds == NULL || datas == NULL)
219 errx(1, "can't allocate proplist");
220 if (!quota2_prop_add_command(cmds, "getall", qfextension[type], datas))
221 err(1, "prop_add_command");
222 if (!quota2_prop_add_command(cmds, "get version", qfextension[type],
223 prop_array_create()))
224 err(1, "prop_add_command");
225 if (!prop_dictionary_set(dict, "commands", cmds))
226 err(1, "prop_dictionary_set(command)");
227 if (Dflag)
228 printf("message to kernel:\n%s\n",
229 prop_dictionary_externalize(dict));
230 if (!prop_dictionary_send_syscall(dict, &pref))
231 err(1, "prop_dictionary_send_syscall");
232 prop_object_release(dict);
233
234 if (quotactl(vfs->f_mntonname, &pref) != 0)
235 err(1, "quotactl");
236
237 if ((errno = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
238 err(1, "prop_dictionary_recv_syscall");
239 }
240 if (Dflag)
241 printf("reply from kernel:\n%s\n",
242 prop_dictionary_externalize(dict));
243 if ((errno = quota2_get_cmds(dict, &cmds)) != 0) {
244 err(1, "quota2_get_cmds");
245 }
246 cmditer = prop_array_iterator(cmds);
247 if (cmditer == NULL)
248 err(1, "prop_array_iterator(cmds)");
249
250 while ((cmd = prop_object_iterator_next(cmditer)) != NULL) {
251 const char *cmdstr;
252 if (!prop_dictionary_get_cstring_nocopy(cmd, "command",
253 &cmdstr))
254 err(1, "prop_get(command)");
255
256 if (!prop_dictionary_get_int8(cmd, "return", &error8))
257 err(1, "prop_get(return)");
258
259 if (error8) {
260 prop_object_release(dict);
261 if (error8 != EOPNOTSUPP) {
262 errno = error8;
263 warn("get %s quotas", qfextension[type]);
264 }
265 return error8;
266 }
267 datas = prop_dictionary_get(cmd, "data");
268 if (datas == NULL)
269 err(1, "prop_dict_get(datas)");
270
271 if (strcmp("get version", cmdstr) == 0) {
272 data = prop_array_get(datas, 0);
273 if (data == NULL)
274 err(1, "prop_array_get(version)");
275 if (!prop_dictionary_get_int8(data, "version",
276 &version))
277 err(1, "prop_get_int8(version)");
278 continue;
279 }
280 dataiter = prop_array_iterator(datas);
281 if (dataiter == NULL)
282 err(1, "prop_array_iterator");
283
284 valid[type] = 1;
285 while ((data = prop_object_iterator_next(dataiter)) != NULL) {
286 strid = NULL;
287 if (!prop_dictionary_get_uint32(data, "id", &id)) {
288 if (!prop_dictionary_get_cstring_nocopy(data,
289 "id", &strid))
290 errx(1, "can't find id in quota entry");
291 if (strcmp(strid, "default") != 0) {
292 errx(1,
293 "wrong id string %s in quota entry",
294 strid);
295 }
296 q2ep = &defaultq2e[type];
297 } else {
298 if ((fup = lookup(id, type)) == 0)
299 fup = addid(id, type, (char *)0);
300 q2ep = &fup->fu_q2e;
301 q2ep->q2e_uid = id;
302 }
303
304 errno = quota2_dict_get_q2e_usage(data, q2ep);
305 if (errno)
306 err(1, "quota2_dict_get_q2e_usage");
307 }
308 prop_object_iterator_release(dataiter);
309 }
310 prop_object_iterator_release(cmditer);
311 prop_object_release(dict);
312 if (xflag == 0)
313 printquotas(type, vfs, version);
314 return 0;
315 }
316
317 static int
318 repquota1(const struct statvfs *vfs, int type)
319 {
320 char qfpathname[MAXPATHLEN];
321 struct fstab *fs;
322 struct fileusage *fup;
323 FILE *qf;
324 uint32_t id;
325 struct dqblk dqbuf;
326 time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
327
328 setfsent();
329 while ((fs = getfsent()) != NULL) {
330 if (strcmp(fs->fs_vfstype, "ffs") == 0 &&
331 strcmp(fs->fs_file, vfs->f_mntonname) == 0)
332 break;
333 }
334 endfsent();
335 if (fs == NULL) {
336 warnx("%s not found in fstab", vfs->f_mntonname);
337 return 1;
338 }
339 if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
340 return 0;
341
342 if ((qf = fopen(qfpathname, "r")) == NULL) {
343 warn("Cannot open `%s'", qfpathname);
344 return 1;
345 }
346 for (id = 0; ; id++) {
347 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
348 if (feof(qf))
349 break;
350 if (id == 0) {
351 if (dqbuf.dqb_btime > 0)
352 bgrace = dqbuf.dqb_btime;
353 if (dqbuf.dqb_itime > 0)
354 igrace = dqbuf.dqb_itime;
355 }
356 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0 &&
357 dqbuf.dqb_bsoftlimit == 0 && dqbuf.dqb_bhardlimit == 0 &&
358 dqbuf.dqb_isoftlimit == 0 && dqbuf.dqb_ihardlimit == 0)
359 continue;
360 if ((fup = lookup(id, type)) == 0)
361 fup = addid(id, type, (char *)0);
362 dqblk2q2e(&dqbuf, &fup->fu_q2e);
363 fup->fu_q2e.q2e_val[QL_BLOCK].q2v_grace = bgrace;
364 fup->fu_q2e.q2e_val[QL_FILE].q2v_grace = igrace;
365 }
366 defaultq2e[type].q2e_val[QL_BLOCK].q2v_grace = bgrace;
367 defaultq2e[type].q2e_val[QL_FILE].q2v_grace = igrace;
368 defaultq2e[type].q2e_val[QL_BLOCK].q2v_softlimit =
369 defaultq2e[type].q2e_val[QL_BLOCK].q2v_hardlimit =
370 defaultq2e[type].q2e_val[QL_FILE].q2v_softlimit =
371 defaultq2e[type].q2e_val[QL_FILE].q2v_hardlimit = UQUAD_MAX;
372 fclose(qf);
373 valid[type] = 1;
374 if (xflag == 0)
375 printquotas(type, vfs, 1);
376 return 0;
377 }
378
379 static void
380 printquotas(int type, const struct statvfs *vfs, int version)
381 {
382 static int multiple = 0;
383 uint32_t id;
384 int i;
385 struct fileusage *fup;
386 struct quota2_val *q;
387 const char *timemsg[N_QL];
388 char overchar[N_QL];
389 time_t now;
390 char b0[20], b1[20], b2[20], b3[20];
391
392 switch(type) {
393 case GRPQUOTA:
394 {
395 struct group *gr;
396 setgrent();
397 while ((gr = getgrent()) != 0)
398 (void)addid(gr->gr_gid, GRPQUOTA, gr->gr_name);
399 endgrent();
400 break;
401 }
402 case USRQUOTA:
403 {
404 struct passwd *pw;
405 setpwent();
406 while ((pw = getpwent()) != 0)
407 (void)addid(pw->pw_uid, USRQUOTA, pw->pw_name);
408 endpwent();
409 break;
410 }
411 default:
412 errx(1, "unknown quota type %d", type);
413 }
414
415 time(&now);
416
417 if (multiple++)
418 printf("\n");
419 if (vflag)
420 printf("*** Report for %s quotas on %s (%s, version %d)\n",
421 qfextension[type], vfs->f_mntonname, vfs->f_mntfromname,
422 version);
423 printf(" Block limits "
424 "File limits\n");
425 printf(type == USRQUOTA ? "User " : "Group");
426 printf(" used soft hard grace used"
427 "soft hard grace\n");
428 for (id = 0; id <= highid[type]; id++) {
429 fup = qremove(id, type);
430 q = fup->fu_q2e.q2e_val;
431 if (fup == 0)
432 continue;
433 for (i = 0; i < N_QL; i++) {
434 switch (QL_STATUS(quota2_check_limit(&q[i], 1, now))) {
435 case QL_S_DENY_HARD:
436 case QL_S_DENY_GRACE:
437 case QL_S_ALLOW_SOFT:
438 timemsg[i] = timeprt(b0, 8, now, q[i].q2v_time);
439 overchar[i] = '+';
440 break;
441 default:
442 timemsg[i] = (vflag && version == 2) ?
443 timeprt(b0, 8, 0, q[i].q2v_grace) : "";
444 overchar[i] = '-';
445 break;
446 }
447 }
448
449 if (q[QL_BLOCK].q2v_cur == 0 &&
450 q[QL_FILE].q2v_cur == 0 && vflag == 0 &&
451 overchar[QL_BLOCK] == '-' && overchar[QL_FILE] == '-')
452 continue;
453 if (strlen(fup->fu_name) > 9)
454 printf("%s ", fup->fu_name);
455 else
456 printf("%-10s", fup->fu_name);
457 printf("%c%c%9s%9s%9s%7s",
458 overchar[QL_BLOCK], overchar[QL_FILE],
459 intprt(b1, 10, q[QL_BLOCK].q2v_cur, HN_B, hflag),
460 intprt(b2, 10, q[QL_BLOCK].q2v_softlimit, HN_B, hflag),
461 intprt(b3, 10, q[QL_BLOCK].q2v_hardlimit, HN_B, hflag),
462 timemsg[QL_BLOCK]);
463 printf(" %8s%8s%8s%7s\n",
464 intprt(b1, 9, q[QL_FILE].q2v_cur, 0, hflag),
465 intprt(b2, 9, q[QL_FILE].q2v_softlimit, 0, hflag),
466 intprt(b3, 9, q[QL_FILE].q2v_hardlimit, 0, hflag),
467 timemsg[QL_FILE]);
468 free(fup);
469 }
470 }
471
472 static void
473 exportquotas(void)
474 {
475 uint32_t id;
476 struct fileusage *fup;
477 prop_dictionary_t dict, data;
478 prop_array_t cmds, datas;
479 int type;
480
481 dict = quota2_prop_create();
482 cmds = prop_array_create();
483
484 if (dict == NULL || cmds == NULL) {
485 errx(1, "can't allocate proplist");
486 }
487
488
489 for (type = 0; type < MAXQUOTAS; type++) {
490 if (valid[type] == 0)
491 continue;
492 datas = prop_array_create();
493 if (datas == NULL)
494 errx(1, "can't allocate proplist");
495 data = q2etoprop(&defaultq2e[type], 1);
496 if (data == NULL)
497 err(1, "q2etoprop(default)");
498 if (!prop_array_add_and_rel(datas, data))
499 err(1, "prop_array_add(data)");
500
501 for (id = 0; id <= highid[type]; id++) {
502 fup = qremove(id, type);
503 if (fup == 0)
504 continue;
505 fup->fu_q2e.q2e_uid = id;
506 data = q2etoprop(&fup->fu_q2e, 0);
507 if (data == NULL)
508 err(1, "q2etoprop(default)");
509 if (!prop_array_add_and_rel(datas, data))
510 err(1, "prop_array_add(data)");
511 free(fup);
512 }
513
514 if (!quota2_prop_add_command(cmds, "set",
515 qfextension[type], datas))
516 err(1, "prop_add_command");
517 }
518
519 if (!prop_dictionary_set(dict, "commands", cmds))
520 err(1, "prop_dictionary_set(command)");
521
522 printf("%s\n", prop_dictionary_externalize(dict));
523 return;
524 }
525
526 /*
527 * Routines to manage the file usage table.
528 *
529 * Lookup an id of a specific type.
530 */
531 struct fileusage *
532 lookup(uint32_t id, int type)
533 {
534 struct fileusage *fup;
535
536 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
537 if (fup->fu_id == id)
538 return fup;
539 return NULL;
540 }
541 /*
542 * Lookup and remove an id of a specific type.
543 */
544 static struct fileusage *
545 qremove(uint32_t id, int type)
546 {
547 struct fileusage *fup, **fupp;
548
549 for (fupp = &fuhead[type][id & (FUHASH-1)]; *fupp != 0;) {
550 fup = *fupp;
551 if (fup->fu_id == id) {
552 *fupp = fup->fu_next;
553 return fup;
554 }
555 fupp = &fup->fu_next;
556 }
557 return NULL;
558 }
559
560 /*
561 * Add a new file usage id if it does not already exist.
562 */
563 static struct fileusage *
564 addid(uint32_t id, int type, const char *name)
565 {
566 struct fileusage *fup, **fhp;
567 struct group *gr = NULL;
568 struct passwd *pw = NULL;
569 size_t len;
570
571 if ((fup = lookup(id, type)) != NULL) {
572 return fup;
573 }
574 if (name == NULL) {
575 switch(type) {
576 case GRPQUOTA:
577 gr = getgrgid(id);
578
579 if (gr != NULL)
580 name = gr->gr_name;
581 break;
582 case USRQUOTA:
583 pw = getpwuid(id);
584 if (pw)
585 name = pw->pw_name;
586 break;
587 default:
588 errx(1, "unknown quota type %d\n", type);
589 }
590 }
591
592 if (name)
593 len = strlen(name);
594 else
595 len = 10;
596 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
597 err(1, "out of memory for fileusage structures");
598 fhp = &fuhead[type][id & (FUHASH - 1)];
599 fup->fu_next = *fhp;
600 *fhp = fup;
601 fup->fu_id = id;
602 if (id > highid[type])
603 highid[type] = id;
604 if (name) {
605 memmove(fup->fu_name, name, len + 1);
606 } else {
607 snprintf(fup->fu_name, len + 1, "%u", id);
608 }
609 fup->fu_q2e = defaultq2e[type];
610 return fup;
611 }
612