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