repquota.c revision 1.35 1 1.35 dholland /* $NetBSD: repquota.c,v 1.35 2012/01/09 15:38:59 dholland Exp $ */
2 1.27 christos
3 1.1 cgd /*
4 1.4 mycroft * Copyright (c) 1980, 1990, 1993
5 1.4 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Elz at The University of Melbourne.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.20 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.10 mrg #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.23 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.23 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.10 mrg #if 0
43 1.10 mrg static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
44 1.10 mrg #else
45 1.35 dholland __RCSID("$NetBSD: repquota.c,v 1.35 2012/01/09 15:38:59 dholland Exp $");
46 1.10 mrg #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Quota report
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.1 cgd #include <sys/stat.h>
54 1.26 bouyer #include <sys/types.h>
55 1.26 bouyer #include <sys/statvfs.h>
56 1.26 bouyer
57 1.11 lukem #include <errno.h>
58 1.26 bouyer #include <err.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.30 bouyer #include <quota/quota.h>
68 1.26 bouyer #include <ufs/ufs/quota1.h>
69 1.34 dholland #include <quota.h>
70 1.26 bouyer
71 1.27 christos #include "printquota.h"
72 1.27 christos #include "quotautil.h"
73 1.1 cgd
74 1.1 cgd struct fileusage {
75 1.1 cgd struct fileusage *fu_next;
76 1.33 dholland struct quotaval fu_qv[QUOTA_NLIMITS];
77 1.27 christos uint32_t fu_id;
78 1.1 cgd char fu_name[1];
79 1.1 cgd /* actually bigger */
80 1.1 cgd };
81 1.1 cgd #define FUHASH 1024 /* must be power of two */
82 1.30 bouyer static struct fileusage *fuhead[QUOTA_NCLASS][FUHASH];
83 1.30 bouyer static uint32_t highid[QUOTA_NCLASS]; /* highest addid()'ed identifier per class */
84 1.30 bouyer int valid[QUOTA_NCLASS];
85 1.33 dholland static struct quotaval defaultqv[QUOTA_NCLASS][QUOTA_NLIMITS];
86 1.1 cgd
87 1.27 christos static int vflag = 0; /* verbose */
88 1.27 christos static int aflag = 0; /* all file systems */
89 1.27 christos static int Dflag = 0; /* debug */
90 1.27 christos static int hflag = 0; /* humanize */
91 1.27 christos static int xflag = 0; /* export */
92 1.27 christos
93 1.27 christos
94 1.27 christos static struct fileusage *addid(uint32_t, int, const char *);
95 1.27 christos static struct fileusage *lookup(uint32_t, int);
96 1.27 christos static struct fileusage *qremove(uint32_t, int);
97 1.35 dholland static int repquota(struct quotahandle *, int);
98 1.35 dholland static int repquota2(struct quotahandle *, int);
99 1.35 dholland static int repquota1(struct quotahandle *, int);
100 1.27 christos static void usage(void) __attribute__((__noreturn__));
101 1.35 dholland static void printquotas(int, struct quotahandle *);
102 1.27 christos static void exportquotas(void);
103 1.11 lukem
104 1.11 lukem int
105 1.27 christos main(int argc, char **argv)
106 1.1 cgd {
107 1.1 cgd int gflag = 0, uflag = 0, errs = 0;
108 1.1 cgd long i, argnum, done = 0;
109 1.9 mark int ch;
110 1.26 bouyer struct statvfs *fst;
111 1.26 bouyer int nfst;
112 1.35 dholland struct quotahandle *qh;
113 1.1 cgd
114 1.26 bouyer while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
115 1.1 cgd switch(ch) {
116 1.1 cgd case 'a':
117 1.1 cgd aflag++;
118 1.1 cgd break;
119 1.1 cgd case 'g':
120 1.1 cgd gflag++;
121 1.1 cgd break;
122 1.1 cgd case 'u':
123 1.1 cgd uflag++;
124 1.1 cgd break;
125 1.26 bouyer case 'h':
126 1.26 bouyer hflag++;
127 1.26 bouyer break;
128 1.1 cgd case 'v':
129 1.1 cgd vflag++;
130 1.1 cgd break;
131 1.26 bouyer case 'D':
132 1.26 bouyer Dflag++;
133 1.26 bouyer break;
134 1.26 bouyer case 'x':
135 1.26 bouyer xflag++;
136 1.26 bouyer break;
137 1.1 cgd default:
138 1.1 cgd usage();
139 1.1 cgd }
140 1.1 cgd }
141 1.1 cgd argc -= optind;
142 1.1 cgd argv += optind;
143 1.26 bouyer if (xflag && (argc != 1 || aflag))
144 1.26 bouyer usage();
145 1.1 cgd if (argc == 0 && !aflag)
146 1.1 cgd usage();
147 1.1 cgd if (!gflag && !uflag) {
148 1.1 cgd if (aflag)
149 1.1 cgd gflag++;
150 1.1 cgd uflag++;
151 1.1 cgd }
152 1.26 bouyer
153 1.26 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
154 1.26 bouyer if (nfst == 0)
155 1.27 christos errx(1, "no filesystems mounted!");
156 1.26 bouyer for (i = 0; i < nfst; i++) {
157 1.26 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
158 1.1 cgd continue;
159 1.35 dholland /* check if we want this volume */
160 1.35 dholland if (!aflag) {
161 1.35 dholland argnum = oneof(fst[i].f_mntonname, argv, argc);
162 1.35 dholland if (argnum < 0) {
163 1.35 dholland argnum = oneof(fst[i].f_mntfromname,
164 1.35 dholland argv, argc);
165 1.35 dholland }
166 1.35 dholland if (argnum < 0) {
167 1.35 dholland continue;
168 1.35 dholland }
169 1.35 dholland done |= 1U << argnum;
170 1.35 dholland }
171 1.35 dholland
172 1.35 dholland qh = quota_open(fst[i].f_mntonname);
173 1.35 dholland if (qh == NULL) {
174 1.35 dholland /* XXX: check this errno */
175 1.35 dholland if (errno == EOPNOTSUPP || errno == ENXIO) {
176 1.35 dholland continue;
177 1.35 dholland }
178 1.35 dholland warn("%s: quota_open", fst[i].f_mntonname);
179 1.1 cgd continue;
180 1.1 cgd }
181 1.35 dholland
182 1.35 dholland if (gflag)
183 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_GROUP);
184 1.35 dholland if (uflag)
185 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_USER);
186 1.35 dholland
187 1.35 dholland quota_close(qh);
188 1.1 cgd }
189 1.26 bouyer if (xflag)
190 1.26 bouyer exportquotas();
191 1.1 cgd for (i = 0; i < argc; i++)
192 1.27 christos if ((done & (1U << i)) == 0)
193 1.27 christos warnx("%s not mounted", argv[i]);
194 1.27 christos return errs;
195 1.1 cgd }
196 1.1 cgd
197 1.27 christos static void
198 1.27 christos usage(void)
199 1.27 christos {
200 1.27 christos const char *p = getprogname();
201 1.27 christos fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
202 1.27 christos "\t%s [-D] [-v] [-g] [-u] filesys ...\n"
203 1.27 christos "\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
204 1.1 cgd exit(1);
205 1.1 cgd }
206 1.1 cgd
207 1.27 christos static int
208 1.35 dholland repquota(struct quotahandle *qh, int idtype)
209 1.26 bouyer {
210 1.35 dholland if (repquota2(qh, idtype) != 0)
211 1.35 dholland return repquota1(qh, idtype);
212 1.26 bouyer return 0;
213 1.26 bouyer }
214 1.26 bouyer
215 1.27 christos static int
216 1.35 dholland repquota2_getstuff(struct quotahandle *qh, int idtype, prop_array_t *ret)
217 1.26 bouyer {
218 1.35 dholland prop_dictionary_t dict, cmd;
219 1.26 bouyer prop_array_t cmds, datas;
220 1.26 bouyer struct plistref pref;
221 1.35 dholland int8_t error8;
222 1.26 bouyer
223 1.30 bouyer dict = quota_prop_create();
224 1.26 bouyer cmds = prop_array_create();
225 1.26 bouyer datas = prop_array_create();
226 1.26 bouyer
227 1.26 bouyer if (dict == NULL || cmds == NULL || datas == NULL)
228 1.26 bouyer errx(1, "can't allocate proplist");
229 1.30 bouyer if (!quota_prop_add_command(cmds, "getall",
230 1.34 dholland ufs_quota_class_names[idtype], datas))
231 1.26 bouyer err(1, "prop_add_command");
232 1.26 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
233 1.26 bouyer err(1, "prop_dictionary_set(command)");
234 1.26 bouyer if (Dflag)
235 1.26 bouyer printf("message to kernel:\n%s\n",
236 1.26 bouyer prop_dictionary_externalize(dict));
237 1.32 jym if (prop_dictionary_send_syscall(dict, &pref) != 0)
238 1.26 bouyer err(1, "prop_dictionary_send_syscall");
239 1.26 bouyer prop_object_release(dict);
240 1.26 bouyer
241 1.35 dholland if (quotactl(quota_getmountpoint(qh), &pref) != 0)
242 1.26 bouyer err(1, "quotactl");
243 1.26 bouyer
244 1.32 jym if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
245 1.27 christos err(1, "prop_dictionary_recv_syscall");
246 1.26 bouyer }
247 1.26 bouyer if (Dflag)
248 1.26 bouyer printf("reply from kernel:\n%s\n",
249 1.26 bouyer prop_dictionary_externalize(dict));
250 1.30 bouyer if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
251 1.30 bouyer err(1, "quota_get_cmds");
252 1.26 bouyer }
253 1.35 dholland
254 1.35 dholland cmd = prop_array_get(cmds, 0);
255 1.35 dholland if (cmd == NULL) {
256 1.35 dholland err(1, "prop_array_get(cmds)");
257 1.35 dholland }
258 1.35 dholland
259 1.35 dholland const char *cmdstr;
260 1.35 dholland if (!prop_dictionary_get_cstring_nocopy(cmd, "command",
261 1.35 dholland &cmdstr))
262 1.35 dholland err(1, "prop_get(command)");
263 1.35 dholland
264 1.35 dholland if (!prop_dictionary_get_int8(cmd, "return", &error8))
265 1.35 dholland err(1, "prop_get(return)");
266 1.35 dholland
267 1.35 dholland if (error8) {
268 1.35 dholland prop_object_release(dict);
269 1.35 dholland if (error8 != EOPNOTSUPP) {
270 1.35 dholland errno = error8;
271 1.35 dholland warn("get %s quotas",
272 1.35 dholland ufs_quota_class_names[idtype]);
273 1.26 bouyer }
274 1.35 dholland return 1;
275 1.35 dholland }
276 1.35 dholland datas = prop_dictionary_get(cmd, "data");
277 1.35 dholland if (datas == NULL)
278 1.35 dholland err(1, "prop_dict_get(datas)");
279 1.35 dholland
280 1.35 dholland prop_object_retain(datas);
281 1.35 dholland prop_object_release(dict);
282 1.35 dholland
283 1.35 dholland *ret = datas;
284 1.35 dholland return 0;
285 1.35 dholland }
286 1.35 dholland
287 1.35 dholland static int
288 1.35 dholland repquota2(struct quotahandle *qh, int idtype)
289 1.35 dholland {
290 1.35 dholland prop_dictionary_t data;
291 1.35 dholland prop_array_t datas;
292 1.35 dholland prop_object_iterator_t dataiter;
293 1.35 dholland struct quotaval *qvp;
294 1.35 dholland struct fileusage *fup;
295 1.35 dholland const char *strid;
296 1.35 dholland uint32_t id;
297 1.35 dholland uint64_t *values[QUOTA_NLIMITS];
298 1.35 dholland
299 1.35 dholland if (repquota2_getstuff(qh, idtype, &datas)) {
300 1.35 dholland return 1;
301 1.35 dholland }
302 1.26 bouyer
303 1.26 bouyer dataiter = prop_array_iterator(datas);
304 1.26 bouyer if (dataiter == NULL)
305 1.26 bouyer err(1, "prop_array_iterator");
306 1.26 bouyer
307 1.34 dholland valid[idtype] = 0;
308 1.26 bouyer while ((data = prop_object_iterator_next(dataiter)) != NULL) {
309 1.34 dholland valid[idtype] = 1;
310 1.26 bouyer strid = NULL;
311 1.26 bouyer if (!prop_dictionary_get_uint32(data, "id", &id)) {
312 1.26 bouyer if (!prop_dictionary_get_cstring_nocopy(data,
313 1.26 bouyer "id", &strid))
314 1.26 bouyer errx(1, "can't find id in quota entry");
315 1.26 bouyer if (strcmp(strid, "default") != 0) {
316 1.26 bouyer errx(1,
317 1.26 bouyer "wrong id string %s in quota entry",
318 1.26 bouyer strid);
319 1.26 bouyer }
320 1.34 dholland qvp = defaultqv[idtype];
321 1.26 bouyer } else {
322 1.34 dholland if ((fup = lookup(id, idtype)) == 0)
323 1.34 dholland fup = addid(id, idtype, (char *)0);
324 1.33 dholland qvp = fup->fu_qv;
325 1.26 bouyer }
326 1.30 bouyer values[QUOTA_LIMIT_BLOCK] =
327 1.33 dholland &qvp[QUOTA_LIMIT_BLOCK].qv_hardlimit;
328 1.30 bouyer values[QUOTA_LIMIT_FILE] =
329 1.33 dholland &qvp[QUOTA_LIMIT_FILE].qv_hardlimit;
330 1.26 bouyer
331 1.30 bouyer errno = proptoquota64(data, values,
332 1.30 bouyer ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
333 1.30 bouyer ufs_quota_limit_names, QUOTA_NLIMITS);
334 1.27 christos if (errno)
335 1.30 bouyer err(1, "proptoquota64");
336 1.26 bouyer }
337 1.26 bouyer prop_object_iterator_release(dataiter);
338 1.35 dholland
339 1.34 dholland if (xflag == 0 && valid[idtype])
340 1.35 dholland printquotas(idtype, qh);
341 1.35 dholland
342 1.35 dholland prop_object_release(datas);
343 1.35 dholland
344 1.27 christos return 0;
345 1.26 bouyer }
346 1.26 bouyer
347 1.27 christos static int
348 1.35 dholland repquota1(struct quotahandle *qh, int idtype)
349 1.26 bouyer {
350 1.27 christos char qfpathname[MAXPATHLEN];
351 1.11 lukem struct fstab *fs;
352 1.11 lukem struct fileusage *fup;
353 1.1 cgd FILE *qf;
354 1.27 christos uint32_t id;
355 1.1 cgd struct dqblk dqbuf;
356 1.26 bouyer time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
357 1.34 dholland int type = ufsclass2qtype(idtype);
358 1.35 dholland const char *mountpoint;
359 1.35 dholland
360 1.35 dholland mountpoint = quota_getmountpoint(qh);
361 1.1 cgd
362 1.26 bouyer setfsent();
363 1.26 bouyer while ((fs = getfsent()) != NULL) {
364 1.26 bouyer if (strcmp(fs->fs_vfstype, "ffs") == 0 &&
365 1.35 dholland strcmp(fs->fs_file, mountpoint) == 0)
366 1.26 bouyer break;
367 1.1 cgd }
368 1.26 bouyer endfsent();
369 1.26 bouyer if (fs == NULL) {
370 1.35 dholland warnx("%s not found in fstab", mountpoint);
371 1.26 bouyer return 1;
372 1.26 bouyer }
373 1.27 christos if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
374 1.26 bouyer return 0;
375 1.26 bouyer
376 1.1 cgd if ((qf = fopen(qfpathname, "r")) == NULL) {
377 1.27 christos warn("Cannot open `%s'", qfpathname);
378 1.27 christos return 1;
379 1.1 cgd }
380 1.1 cgd for (id = 0; ; id++) {
381 1.1 cgd fread(&dqbuf, sizeof(struct dqblk), 1, qf);
382 1.1 cgd if (feof(qf))
383 1.1 cgd break;
384 1.26 bouyer if (id == 0) {
385 1.26 bouyer if (dqbuf.dqb_btime > 0)
386 1.26 bouyer bgrace = dqbuf.dqb_btime;
387 1.26 bouyer if (dqbuf.dqb_itime > 0)
388 1.26 bouyer igrace = dqbuf.dqb_itime;
389 1.26 bouyer }
390 1.26 bouyer if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0 &&
391 1.26 bouyer dqbuf.dqb_bsoftlimit == 0 && dqbuf.dqb_bhardlimit == 0 &&
392 1.26 bouyer dqbuf.dqb_isoftlimit == 0 && dqbuf.dqb_ihardlimit == 0)
393 1.1 cgd continue;
394 1.34 dholland if ((fup = lookup(id, idtype)) == 0)
395 1.34 dholland fup = addid(id, idtype, (char *)0);
396 1.33 dholland dqblk_to_quotaval(&dqbuf, fup->fu_qv);
397 1.33 dholland fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_grace = bgrace;
398 1.33 dholland fup->fu_qv[QUOTA_LIMIT_FILE].qv_grace = igrace;
399 1.33 dholland }
400 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_grace = bgrace;
401 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_FILE].qv_grace = igrace;
402 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_softlimit =
403 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit =
404 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_FILE].qv_softlimit =
405 1.34 dholland defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit = QUOTA_NOLIMIT;
406 1.26 bouyer fclose(qf);
407 1.34 dholland valid[idtype] = 1;
408 1.26 bouyer if (xflag == 0)
409 1.35 dholland printquotas(idtype, qh);
410 1.27 christos return 0;
411 1.26 bouyer }
412 1.26 bouyer
413 1.27 christos static void
414 1.35 dholland printquotas(int idtype, struct quotahandle *qh)
415 1.26 bouyer {
416 1.26 bouyer static int multiple = 0;
417 1.27 christos uint32_t id;
418 1.26 bouyer int i;
419 1.26 bouyer struct fileusage *fup;
420 1.33 dholland struct quotaval *q;
421 1.30 bouyer const char *timemsg[QUOTA_NLIMITS];
422 1.30 bouyer char overchar[QUOTA_NLIMITS];
423 1.27 christos time_t now;
424 1.29 bouyer char b0[2][20], b1[20], b2[20], b3[20];
425 1.26 bouyer
426 1.34 dholland switch (idtype) {
427 1.34 dholland case QUOTA_IDTYPE_GROUP:
428 1.26 bouyer {
429 1.26 bouyer struct group *gr;
430 1.26 bouyer setgrent();
431 1.26 bouyer while ((gr = getgrent()) != 0)
432 1.34 dholland (void)addid(gr->gr_gid, idtype, gr->gr_name);
433 1.26 bouyer endgrent();
434 1.26 bouyer break;
435 1.26 bouyer }
436 1.34 dholland case QUOTA_IDTYPE_USER:
437 1.26 bouyer {
438 1.26 bouyer struct passwd *pw;
439 1.26 bouyer setpwent();
440 1.26 bouyer while ((pw = getpwent()) != 0)
441 1.34 dholland (void)addid(pw->pw_uid, idtype, pw->pw_name);
442 1.26 bouyer endpwent();
443 1.26 bouyer break;
444 1.26 bouyer }
445 1.26 bouyer default:
446 1.34 dholland errx(1, "Unknown quota ID type %d", idtype);
447 1.1 cgd }
448 1.26 bouyer
449 1.27 christos time(&now);
450 1.26 bouyer
451 1.26 bouyer if (multiple++)
452 1.26 bouyer printf("\n");
453 1.26 bouyer if (vflag)
454 1.35 dholland printf("*** Report for %s quotas on %s (%s: %s)\n",
455 1.35 dholland ufs_quota_class_names[idtype], quota_getmountpoint(qh),
456 1.35 dholland quota_getmountdevice(qh), quota_getimplname(qh));
457 1.27 christos printf(" Block limits "
458 1.27 christos "File limits\n");
459 1.34 dholland printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
460 1.27 christos printf(" used soft hard grace used"
461 1.27 christos "soft hard grace\n");
462 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
463 1.34 dholland fup = qremove(id, idtype);
464 1.33 dholland q = fup->fu_qv;
465 1.1 cgd if (fup == 0)
466 1.1 cgd continue;
467 1.30 bouyer for (i = 0; i < QUOTA_NLIMITS; i++) {
468 1.33 dholland switch (QL_STATUS(quota_check_limit(q[i].qv_usage, 1,
469 1.33 dholland q[i].qv_softlimit, q[i].qv_hardlimit,
470 1.33 dholland q[i].qv_expiretime, now))) {
471 1.26 bouyer case QL_S_DENY_HARD:
472 1.26 bouyer case QL_S_DENY_GRACE:
473 1.26 bouyer case QL_S_ALLOW_SOFT:
474 1.29 bouyer timemsg[i] = timeprt(b0[i], 8, now,
475 1.33 dholland q[i].qv_expiretime);
476 1.26 bouyer overchar[i] = '+';
477 1.26 bouyer break;
478 1.26 bouyer default:
479 1.35 dholland if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
480 1.35 dholland timemsg[i] = timeprt(b0[i], 8, 0,
481 1.35 dholland q[i].qv_grace);
482 1.35 dholland } else {
483 1.35 dholland timemsg[i] = "";
484 1.35 dholland }
485 1.26 bouyer overchar[i] = '-';
486 1.26 bouyer break;
487 1.26 bouyer }
488 1.26 bouyer }
489 1.26 bouyer
490 1.33 dholland if (q[QUOTA_LIMIT_BLOCK].qv_usage == 0 &&
491 1.33 dholland q[QUOTA_LIMIT_FILE].qv_usage == 0 && vflag == 0 &&
492 1.30 bouyer overchar[QUOTA_LIMIT_BLOCK] == '-' &&
493 1.30 bouyer overchar[QUOTA_LIMIT_FILE] == '-')
494 1.1 cgd continue;
495 1.22 jdolecek if (strlen(fup->fu_name) > 9)
496 1.22 jdolecek printf("%s ", fup->fu_name);
497 1.22 jdolecek else
498 1.22 jdolecek printf("%-10s", fup->fu_name);
499 1.26 bouyer printf("%c%c%9s%9s%9s%7s",
500 1.30 bouyer overchar[QUOTA_LIMIT_BLOCK], overchar[QUOTA_LIMIT_FILE],
501 1.33 dholland intprt(b1, 10, q[QUOTA_LIMIT_BLOCK].qv_usage,
502 1.30 bouyer HN_B, hflag),
503 1.33 dholland intprt(b2, 10, q[QUOTA_LIMIT_BLOCK].qv_softlimit,
504 1.30 bouyer HN_B, hflag),
505 1.33 dholland intprt(b3, 10, q[QUOTA_LIMIT_BLOCK].qv_hardlimit,
506 1.30 bouyer HN_B, hflag),
507 1.30 bouyer timemsg[QUOTA_LIMIT_BLOCK]);
508 1.26 bouyer printf(" %8s%8s%8s%7s\n",
509 1.33 dholland intprt(b1, 9, q[QUOTA_LIMIT_FILE].qv_usage, 0, hflag),
510 1.33 dholland intprt(b2, 9, q[QUOTA_LIMIT_FILE].qv_softlimit,
511 1.30 bouyer 0, hflag),
512 1.33 dholland intprt(b3, 9, q[QUOTA_LIMIT_FILE].qv_hardlimit,
513 1.30 bouyer 0, hflag),
514 1.30 bouyer timemsg[QUOTA_LIMIT_FILE]);
515 1.26 bouyer free(fup);
516 1.26 bouyer }
517 1.26 bouyer }
518 1.26 bouyer
519 1.27 christos static void
520 1.27 christos exportquotas(void)
521 1.26 bouyer {
522 1.27 christos uint32_t id;
523 1.26 bouyer struct fileusage *fup;
524 1.26 bouyer prop_dictionary_t dict, data;
525 1.26 bouyer prop_array_t cmds, datas;
526 1.34 dholland int idtype;
527 1.30 bouyer uint64_t *valuesp[QUOTA_NLIMITS];
528 1.26 bouyer
529 1.30 bouyer dict = quota_prop_create();
530 1.26 bouyer cmds = prop_array_create();
531 1.26 bouyer
532 1.26 bouyer if (dict == NULL || cmds == NULL) {
533 1.26 bouyer errx(1, "can't allocate proplist");
534 1.26 bouyer }
535 1.26 bouyer
536 1.26 bouyer
537 1.34 dholland for (idtype = 0; idtype < QUOTA_NCLASS; idtype++) {
538 1.34 dholland if (valid[idtype] == 0)
539 1.26 bouyer continue;
540 1.26 bouyer datas = prop_array_create();
541 1.26 bouyer if (datas == NULL)
542 1.26 bouyer errx(1, "can't allocate proplist");
543 1.30 bouyer valuesp[QUOTA_LIMIT_BLOCK] =
544 1.34 dholland &defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit;
545 1.30 bouyer valuesp[QUOTA_LIMIT_FILE] =
546 1.34 dholland &defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit;
547 1.30 bouyer data = quota64toprop(0, 1, valuesp,
548 1.30 bouyer ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
549 1.30 bouyer ufs_quota_limit_names, QUOTA_NLIMITS);
550 1.26 bouyer if (data == NULL)
551 1.30 bouyer err(1, "quota64toprop(default)");
552 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
553 1.26 bouyer err(1, "prop_array_add(data)");
554 1.26 bouyer
555 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
556 1.34 dholland fup = qremove(id, idtype);
557 1.26 bouyer if (fup == 0)
558 1.26 bouyer continue;
559 1.30 bouyer valuesp[QUOTA_LIMIT_BLOCK] =
560 1.33 dholland &fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_hardlimit;
561 1.30 bouyer valuesp[QUOTA_LIMIT_FILE] =
562 1.33 dholland &fup->fu_qv[QUOTA_LIMIT_FILE].qv_hardlimit;
563 1.30 bouyer data = quota64toprop(id, 0, valuesp,
564 1.30 bouyer ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
565 1.30 bouyer ufs_quota_limit_names, QUOTA_NLIMITS);
566 1.26 bouyer if (data == NULL)
567 1.30 bouyer err(1, "quota64toprop(id)");
568 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
569 1.26 bouyer err(1, "prop_array_add(data)");
570 1.26 bouyer free(fup);
571 1.26 bouyer }
572 1.26 bouyer
573 1.30 bouyer if (!quota_prop_add_command(cmds, "set",
574 1.34 dholland ufs_quota_class_names[idtype], datas))
575 1.26 bouyer err(1, "prop_add_command");
576 1.1 cgd }
577 1.26 bouyer
578 1.26 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
579 1.26 bouyer err(1, "prop_dictionary_set(command)");
580 1.26 bouyer
581 1.26 bouyer printf("%s\n", prop_dictionary_externalize(dict));
582 1.26 bouyer return;
583 1.1 cgd }
584 1.1 cgd
585 1.1 cgd /*
586 1.1 cgd * Routines to manage the file usage table.
587 1.1 cgd *
588 1.34 dholland * Lookup an id of a specific id type.
589 1.1 cgd */
590 1.1 cgd struct fileusage *
591 1.34 dholland lookup(uint32_t id, int idtype)
592 1.1 cgd {
593 1.11 lukem struct fileusage *fup;
594 1.1 cgd
595 1.34 dholland for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
596 1.1 cgd if (fup->fu_id == id)
597 1.27 christos return fup;
598 1.27 christos return NULL;
599 1.1 cgd }
600 1.26 bouyer /*
601 1.34 dholland * Lookup and remove an id of a specific id type.
602 1.26 bouyer */
603 1.27 christos static struct fileusage *
604 1.34 dholland qremove(uint32_t id, int idtype)
605 1.26 bouyer {
606 1.26 bouyer struct fileusage *fup, **fupp;
607 1.26 bouyer
608 1.34 dholland for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
609 1.26 bouyer fup = *fupp;
610 1.26 bouyer if (fup->fu_id == id) {
611 1.26 bouyer *fupp = fup->fu_next;
612 1.27 christos return fup;
613 1.26 bouyer }
614 1.26 bouyer fupp = &fup->fu_next;
615 1.26 bouyer }
616 1.27 christos return NULL;
617 1.26 bouyer }
618 1.1 cgd
619 1.1 cgd /*
620 1.1 cgd * Add a new file usage id if it does not already exist.
621 1.1 cgd */
622 1.27 christos static struct fileusage *
623 1.34 dholland addid(uint32_t id, int idtype, const char *name)
624 1.1 cgd {
625 1.1 cgd struct fileusage *fup, **fhp;
626 1.26 bouyer struct group *gr = NULL;
627 1.26 bouyer struct passwd *pw = NULL;
628 1.27 christos size_t len;
629 1.1 cgd
630 1.34 dholland if ((fup = lookup(id, idtype)) != NULL) {
631 1.27 christos return fup;
632 1.26 bouyer }
633 1.26 bouyer if (name == NULL) {
634 1.34 dholland switch(idtype) {
635 1.34 dholland case QUOTA_IDTYPE_GROUP:
636 1.26 bouyer gr = getgrgid(id);
637 1.26 bouyer
638 1.26 bouyer if (gr != NULL)
639 1.26 bouyer name = gr->gr_name;
640 1.26 bouyer break;
641 1.34 dholland case QUOTA_IDTYPE_USER:
642 1.26 bouyer pw = getpwuid(id);
643 1.26 bouyer if (pw)
644 1.26 bouyer name = pw->pw_name;
645 1.26 bouyer break;
646 1.26 bouyer default:
647 1.34 dholland errx(1, "Unknown quota ID type %d\n", idtype);
648 1.26 bouyer }
649 1.26 bouyer }
650 1.26 bouyer
651 1.1 cgd if (name)
652 1.1 cgd len = strlen(name);
653 1.1 cgd else
654 1.1 cgd len = 10;
655 1.27 christos if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
656 1.27 christos err(1, "out of memory for fileusage structures");
657 1.34 dholland fhp = &fuhead[idtype][id & (FUHASH - 1)];
658 1.1 cgd fup->fu_next = *fhp;
659 1.1 cgd *fhp = fup;
660 1.1 cgd fup->fu_id = id;
661 1.34 dholland if (id > highid[idtype])
662 1.34 dholland highid[idtype] = id;
663 1.1 cgd if (name) {
664 1.12 lukem memmove(fup->fu_name, name, len + 1);
665 1.1 cgd } else {
666 1.28 christos snprintf(fup->fu_name, len + 1, "%u", id);
667 1.1 cgd }
668 1.34 dholland fup->fu_qv[QUOTA_LIMIT_BLOCK] = defaultqv[idtype][QUOTA_LIMIT_BLOCK];
669 1.34 dholland fup->fu_qv[QUOTA_LIMIT_FILE] = defaultqv[idtype][QUOTA_LIMIT_FILE];
670 1.27 christos return fup;
671 1.1 cgd }
672