repquota.c revision 1.32 1 /* $NetBSD: repquota.c,v 1.32 2011/09/30 22:08:20 jym 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.32 2011/09/30 22:08:20 jym 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
57 #include <errno.h>
58 #include <err.h>
59 #include <fstab.h>
60 #include <grp.h>
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include <quota/quotaprop.h>
68 #include <quota/quota.h>
69 #include <ufs/ufs/quota1.h>
70 #include <sys/quota.h>
71
72 #include "printquota.h"
73 #include "quotautil.h"
74
75 struct fileusage {
76 struct fileusage *fu_next;
77 struct ufs_quota_entry fu_qe[QUOTA_NLIMITS];
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[QUOTA_NCLASS][FUHASH];
84 static uint32_t highid[QUOTA_NCLASS]; /* highest addid()'ed identifier per class */
85 int valid[QUOTA_NCLASS];
86 static struct ufs_quota_entry defaultqe[QUOTA_NCLASS][QUOTA_NLIMITS];
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], QUOTA_CLASS_GROUP);
162 if (uflag)
163 errs += repquota(&fst[i], QUOTA_CLASS_USER);
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], QUOTA_CLASS_GROUP);
171 if (uflag)
172 errs += repquota(&fst[i], QUOTA_CLASS_USER);
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 class)
195 {
196 if (repquota2(vfs, class) != 0)
197 return repquota1(vfs, class);
198 return 0;
199 }
200
201 static int
202 repquota2(const struct statvfs *vfs, int class)
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 ufs_quota_entry *qep;
210 struct fileusage *fup;
211 const char *strid;
212 uint32_t id;
213 uint64_t *values[QUOTA_NLIMITS];
214
215 dict = quota_prop_create();
216 cmds = prop_array_create();
217 datas = prop_array_create();
218
219 if (dict == NULL || cmds == NULL || datas == NULL)
220 errx(1, "can't allocate proplist");
221 if (!quota_prop_add_command(cmds, "getall",
222 ufs_quota_class_names[class], datas))
223 err(1, "prop_add_command");
224 if (!quota_prop_add_command(cmds, "get version",
225 ufs_quota_class_names[class], prop_array_create()))
226 err(1, "prop_add_command");
227 if (!prop_dictionary_set(dict, "commands", cmds))
228 err(1, "prop_dictionary_set(command)");
229 if (Dflag)
230 printf("message to kernel:\n%s\n",
231 prop_dictionary_externalize(dict));
232 if (prop_dictionary_send_syscall(dict, &pref) != 0)
233 err(1, "prop_dictionary_send_syscall");
234 prop_object_release(dict);
235
236 if (quotactl(vfs->f_mntonname, &pref) != 0)
237 err(1, "quotactl");
238
239 if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
240 err(1, "prop_dictionary_recv_syscall");
241 }
242 if (Dflag)
243 printf("reply from kernel:\n%s\n",
244 prop_dictionary_externalize(dict));
245 if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
246 err(1, "quota_get_cmds");
247 }
248 cmditer = prop_array_iterator(cmds);
249 if (cmditer == NULL)
250 err(1, "prop_array_iterator(cmds)");
251
252 while ((cmd = prop_object_iterator_next(cmditer)) != NULL) {
253 const char *cmdstr;
254 if (!prop_dictionary_get_cstring_nocopy(cmd, "command",
255 &cmdstr))
256 err(1, "prop_get(command)");
257
258 if (!prop_dictionary_get_int8(cmd, "return", &error8))
259 err(1, "prop_get(return)");
260
261 if (error8) {
262 prop_object_release(dict);
263 if (error8 != EOPNOTSUPP) {
264 errno = error8;
265 warn("get %s quotas",
266 ufs_quota_class_names[class]);
267 }
268 return error8;
269 }
270 datas = prop_dictionary_get(cmd, "data");
271 if (datas == NULL)
272 err(1, "prop_dict_get(datas)");
273
274 if (strcmp("get version", cmdstr) == 0) {
275 data = prop_array_get(datas, 0);
276 if (data == NULL)
277 err(1, "prop_array_get(version)");
278 if (!prop_dictionary_get_int8(data, "version",
279 &version))
280 err(1, "prop_get_int8(version)");
281 continue;
282 }
283 dataiter = prop_array_iterator(datas);
284 if (dataiter == NULL)
285 err(1, "prop_array_iterator");
286
287 valid[class] = 0;
288 while ((data = prop_object_iterator_next(dataiter)) != NULL) {
289 valid[class] = 1;
290 strid = NULL;
291 if (!prop_dictionary_get_uint32(data, "id", &id)) {
292 if (!prop_dictionary_get_cstring_nocopy(data,
293 "id", &strid))
294 errx(1, "can't find id in quota entry");
295 if (strcmp(strid, "default") != 0) {
296 errx(1,
297 "wrong id string %s in quota entry",
298 strid);
299 }
300 qep = defaultqe[class];
301 } else {
302 if ((fup = lookup(id, class)) == 0)
303 fup = addid(id, class, (char *)0);
304 qep = fup->fu_qe;
305 }
306 values[QUOTA_LIMIT_BLOCK] =
307 &qep[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit;
308 values[QUOTA_LIMIT_FILE] =
309 &qep[QUOTA_LIMIT_FILE].ufsqe_hardlimit;
310
311 errno = proptoquota64(data, values,
312 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
313 ufs_quota_limit_names, QUOTA_NLIMITS);
314 if (errno)
315 err(1, "proptoquota64");
316 }
317 prop_object_iterator_release(dataiter);
318 }
319 prop_object_iterator_release(cmditer);
320 prop_object_release(dict);
321 if (xflag == 0 && valid[class])
322 printquotas(class, vfs, version);
323 return 0;
324 }
325
326 static int
327 repquota1(const struct statvfs *vfs, int class)
328 {
329 char qfpathname[MAXPATHLEN];
330 struct fstab *fs;
331 struct fileusage *fup;
332 FILE *qf;
333 uint32_t id;
334 struct dqblk dqbuf;
335 time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
336 int type = ufsclass2qtype(class);
337
338 setfsent();
339 while ((fs = getfsent()) != NULL) {
340 if (strcmp(fs->fs_vfstype, "ffs") == 0 &&
341 strcmp(fs->fs_file, vfs->f_mntonname) == 0)
342 break;
343 }
344 endfsent();
345 if (fs == NULL) {
346 warnx("%s not found in fstab", vfs->f_mntonname);
347 return 1;
348 }
349 if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
350 return 0;
351
352 if ((qf = fopen(qfpathname, "r")) == NULL) {
353 warn("Cannot open `%s'", qfpathname);
354 return 1;
355 }
356 for (id = 0; ; id++) {
357 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
358 if (feof(qf))
359 break;
360 if (id == 0) {
361 if (dqbuf.dqb_btime > 0)
362 bgrace = dqbuf.dqb_btime;
363 if (dqbuf.dqb_itime > 0)
364 igrace = dqbuf.dqb_itime;
365 }
366 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0 &&
367 dqbuf.dqb_bsoftlimit == 0 && dqbuf.dqb_bhardlimit == 0 &&
368 dqbuf.dqb_isoftlimit == 0 && dqbuf.dqb_ihardlimit == 0)
369 continue;
370 if ((fup = lookup(id, class)) == 0)
371 fup = addid(id, class, (char *)0);
372 dqblk2ufsqe(&dqbuf, fup->fu_qe);
373 fup->fu_qe[QUOTA_LIMIT_BLOCK].ufsqe_grace = bgrace;
374 fup->fu_qe[QUOTA_LIMIT_FILE].ufsqe_grace = igrace;
375 }
376 defaultqe[class][QUOTA_LIMIT_BLOCK].ufsqe_grace = bgrace;
377 defaultqe[class][QUOTA_LIMIT_FILE].ufsqe_grace = igrace;
378 defaultqe[class][QUOTA_LIMIT_BLOCK].ufsqe_softlimit =
379 defaultqe[class][QUOTA_LIMIT_BLOCK].ufsqe_hardlimit =
380 defaultqe[class][QUOTA_LIMIT_FILE].ufsqe_softlimit =
381 defaultqe[class][QUOTA_LIMIT_FILE].ufsqe_hardlimit = UQUAD_MAX;
382 fclose(qf);
383 valid[class] = 1;
384 if (xflag == 0)
385 printquotas(class, vfs, 1);
386 return 0;
387 }
388
389 static void
390 printquotas(int class, const struct statvfs *vfs, int version)
391 {
392 static int multiple = 0;
393 uint32_t id;
394 int i;
395 struct fileusage *fup;
396 struct ufs_quota_entry *q;
397 const char *timemsg[QUOTA_NLIMITS];
398 char overchar[QUOTA_NLIMITS];
399 time_t now;
400 char b0[2][20], b1[20], b2[20], b3[20];
401
402 switch(class) {
403 case QUOTA_CLASS_GROUP:
404 {
405 struct group *gr;
406 setgrent();
407 while ((gr = getgrent()) != 0)
408 (void)addid(gr->gr_gid, QUOTA_CLASS_GROUP, gr->gr_name);
409 endgrent();
410 break;
411 }
412 case QUOTA_CLASS_USER:
413 {
414 struct passwd *pw;
415 setpwent();
416 while ((pw = getpwent()) != 0)
417 (void)addid(pw->pw_uid, QUOTA_CLASS_USER, pw->pw_name);
418 endpwent();
419 break;
420 }
421 default:
422 errx(1, "unknown quota class %d", class);
423 }
424
425 time(&now);
426
427 if (multiple++)
428 printf("\n");
429 if (vflag)
430 printf("*** Report for %s quotas on %s (%s, version %d)\n",
431 ufs_quota_class_names[class], vfs->f_mntonname,
432 vfs->f_mntfromname, version);
433 printf(" Block limits "
434 "File limits\n");
435 printf(class == QUOTA_CLASS_USER ? "User " : "Group");
436 printf(" used soft hard grace used"
437 "soft hard grace\n");
438 for (id = 0; id <= highid[class]; id++) {
439 fup = qremove(id, class);
440 q = fup->fu_qe;
441 if (fup == 0)
442 continue;
443 for (i = 0; i < QUOTA_NLIMITS; i++) {
444 switch (QL_STATUS(quota_check_limit(q[i].ufsqe_cur, 1,
445 q[i].ufsqe_softlimit, q[i].ufsqe_hardlimit,
446 q[i].ufsqe_time, now))) {
447 case QL_S_DENY_HARD:
448 case QL_S_DENY_GRACE:
449 case QL_S_ALLOW_SOFT:
450 timemsg[i] = timeprt(b0[i], 8, now,
451 q[i].ufsqe_time);
452 overchar[i] = '+';
453 break;
454 default:
455 timemsg[i] = (vflag && version == 2) ?
456 timeprt(b0[i], 8, 0, q[i].ufsqe_grace) : "";
457 overchar[i] = '-';
458 break;
459 }
460 }
461
462 if (q[QUOTA_LIMIT_BLOCK].ufsqe_cur == 0 &&
463 q[QUOTA_LIMIT_FILE].ufsqe_cur == 0 && vflag == 0 &&
464 overchar[QUOTA_LIMIT_BLOCK] == '-' &&
465 overchar[QUOTA_LIMIT_FILE] == '-')
466 continue;
467 if (strlen(fup->fu_name) > 9)
468 printf("%s ", fup->fu_name);
469 else
470 printf("%-10s", fup->fu_name);
471 printf("%c%c%9s%9s%9s%7s",
472 overchar[QUOTA_LIMIT_BLOCK], overchar[QUOTA_LIMIT_FILE],
473 intprt(b1, 10, q[QUOTA_LIMIT_BLOCK].ufsqe_cur,
474 HN_B, hflag),
475 intprt(b2, 10, q[QUOTA_LIMIT_BLOCK].ufsqe_softlimit,
476 HN_B, hflag),
477 intprt(b3, 10, q[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit,
478 HN_B, hflag),
479 timemsg[QUOTA_LIMIT_BLOCK]);
480 printf(" %8s%8s%8s%7s\n",
481 intprt(b1, 9, q[QUOTA_LIMIT_FILE].ufsqe_cur, 0, hflag),
482 intprt(b2, 9, q[QUOTA_LIMIT_FILE].ufsqe_softlimit,
483 0, hflag),
484 intprt(b3, 9, q[QUOTA_LIMIT_FILE].ufsqe_hardlimit,
485 0, hflag),
486 timemsg[QUOTA_LIMIT_FILE]);
487 free(fup);
488 }
489 }
490
491 static void
492 exportquotas(void)
493 {
494 uint32_t id;
495 struct fileusage *fup;
496 prop_dictionary_t dict, data;
497 prop_array_t cmds, datas;
498 int class;
499 uint64_t *valuesp[QUOTA_NLIMITS];
500
501 dict = quota_prop_create();
502 cmds = prop_array_create();
503
504 if (dict == NULL || cmds == NULL) {
505 errx(1, "can't allocate proplist");
506 }
507
508
509 for (class = 0; class < QUOTA_NCLASS; class++) {
510 if (valid[class] == 0)
511 continue;
512 datas = prop_array_create();
513 if (datas == NULL)
514 errx(1, "can't allocate proplist");
515 valuesp[QUOTA_LIMIT_BLOCK] =
516 &defaultqe[class][QUOTA_LIMIT_BLOCK].ufsqe_hardlimit;
517 valuesp[QUOTA_LIMIT_FILE] =
518 &defaultqe[class][QUOTA_LIMIT_FILE].ufsqe_hardlimit;
519 data = quota64toprop(0, 1, valuesp,
520 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
521 ufs_quota_limit_names, QUOTA_NLIMITS);
522 if (data == NULL)
523 err(1, "quota64toprop(default)");
524 if (!prop_array_add_and_rel(datas, data))
525 err(1, "prop_array_add(data)");
526
527 for (id = 0; id <= highid[class]; id++) {
528 fup = qremove(id, class);
529 if (fup == 0)
530 continue;
531 valuesp[QUOTA_LIMIT_BLOCK] =
532 &fup->fu_qe[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit;
533 valuesp[QUOTA_LIMIT_FILE] =
534 &fup->fu_qe[QUOTA_LIMIT_FILE].ufsqe_hardlimit;
535 data = quota64toprop(id, 0, valuesp,
536 ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
537 ufs_quota_limit_names, QUOTA_NLIMITS);
538 if (data == NULL)
539 err(1, "quota64toprop(id)");
540 if (!prop_array_add_and_rel(datas, data))
541 err(1, "prop_array_add(data)");
542 free(fup);
543 }
544
545 if (!quota_prop_add_command(cmds, "set",
546 ufs_quota_class_names[class], datas))
547 err(1, "prop_add_command");
548 }
549
550 if (!prop_dictionary_set(dict, "commands", cmds))
551 err(1, "prop_dictionary_set(command)");
552
553 printf("%s\n", prop_dictionary_externalize(dict));
554 return;
555 }
556
557 /*
558 * Routines to manage the file usage table.
559 *
560 * Lookup an id of a specific class.
561 */
562 struct fileusage *
563 lookup(uint32_t id, int class)
564 {
565 struct fileusage *fup;
566
567 for (fup = fuhead[class][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
568 if (fup->fu_id == id)
569 return fup;
570 return NULL;
571 }
572 /*
573 * Lookup and remove an id of a specific class.
574 */
575 static struct fileusage *
576 qremove(uint32_t id, int class)
577 {
578 struct fileusage *fup, **fupp;
579
580 for (fupp = &fuhead[class][id & (FUHASH-1)]; *fupp != 0;) {
581 fup = *fupp;
582 if (fup->fu_id == id) {
583 *fupp = fup->fu_next;
584 return fup;
585 }
586 fupp = &fup->fu_next;
587 }
588 return NULL;
589 }
590
591 /*
592 * Add a new file usage id if it does not already exist.
593 */
594 static struct fileusage *
595 addid(uint32_t id, int class, const char *name)
596 {
597 struct fileusage *fup, **fhp;
598 struct group *gr = NULL;
599 struct passwd *pw = NULL;
600 size_t len;
601
602 if ((fup = lookup(id, class)) != NULL) {
603 return fup;
604 }
605 if (name == NULL) {
606 switch(class) {
607 case QUOTA_CLASS_GROUP:
608 gr = getgrgid(id);
609
610 if (gr != NULL)
611 name = gr->gr_name;
612 break;
613 case QUOTA_CLASS_USER:
614 pw = getpwuid(id);
615 if (pw)
616 name = pw->pw_name;
617 break;
618 default:
619 errx(1, "unknown quota class %d\n", class);
620 }
621 }
622
623 if (name)
624 len = strlen(name);
625 else
626 len = 10;
627 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
628 err(1, "out of memory for fileusage structures");
629 fhp = &fuhead[class][id & (FUHASH - 1)];
630 fup->fu_next = *fhp;
631 *fhp = fup;
632 fup->fu_id = id;
633 if (id > highid[class])
634 highid[class] = id;
635 if (name) {
636 memmove(fup->fu_name, name, len + 1);
637 } else {
638 snprintf(fup->fu_name, len + 1, "%u", id);
639 }
640 fup->fu_qe[QUOTA_LIMIT_BLOCK] = defaultqe[class][QUOTA_LIMIT_BLOCK];
641 fup->fu_qe[QUOTA_LIMIT_FILE] = defaultqe[class][QUOTA_LIMIT_FILE];
642 return fup;
643 }
644