Deleted Added
full compact
repquota.c (200174) repquota.c (207736)
1/*
2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/usr.sbin/repquota/repquota.c 200174 2009-12-06 01:29:49Z ed $");
45__FBSDID("$FreeBSD: head/usr.sbin/repquota/repquota.c 207736 2010-05-07 00:41:12Z mckusick $");
46
47/*
48 * Quota report
49 */
50#include <sys/param.h>
51#include <sys/mount.h>
46
47/*
48 * Quota report
49 */
50#include <sys/param.h>
51#include <sys/mount.h>
52
52#include <ufs/ufs/quota.h>
53#include <ufs/ufs/quota.h>
54
53#include <err.h>
54#include <errno.h>
55#include <err.h>
56#include <errno.h>
57#include <fcntl.h>
55#include <fstab.h>
56#include <grp.h>
58#include <fstab.h>
59#include <grp.h>
60#include <libutil.h>
57#include <pwd.h>
61#include <pwd.h>
62#include <stdint.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <time.h>
62#include <unistd.h>
63
64/* Let's be paranoid about block size */
65#if 10 > DEV_BSHIFT

--- 8 unchanged lines hidden (view full) ---

74
75#define max(a,b) ((a) >= (b) ? (a) : (b))
76
77const char *qfname = QUOTAFILENAME;
78const char *qfextension[] = INITQFNAMES;
79
80struct fileusage {
81 struct fileusage *fu_next;
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
68
69/* Let's be paranoid about block size */
70#if 10 > DEV_BSHIFT

--- 8 unchanged lines hidden (view full) ---

79
80#define max(a,b) ((a) >= (b) ? (a) : (b))
81
82const char *qfname = QUOTAFILENAME;
83const char *qfextension[] = INITQFNAMES;
84
85struct fileusage {
86 struct fileusage *fu_next;
82 struct dqblk fu_dqblk;
83 u_long fu_id;
84 char fu_name[1];
85 /* actually bigger */
86};
87#define FUHASH 1024 /* must be power of two */
88struct fileusage *fuhead[MAXQUOTAS][FUHASH];
89struct fileusage *lookup(u_long, int);
90struct fileusage *addid(u_long, int, char *);
91u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
92
93int vflag; /* verbose */
94int aflag; /* all filesystems */
95int nflag; /* display user/group by id */
87 u_long fu_id;
88 char fu_name[1];
89 /* actually bigger */
90};
91#define FUHASH 1024 /* must be power of two */
92struct fileusage *fuhead[MAXQUOTAS][FUHASH];
93struct fileusage *lookup(u_long, int);
94struct fileusage *addid(u_long, int, char *);
95u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
96
97int vflag; /* verbose */
98int aflag; /* all filesystems */
99int nflag; /* display user/group by id */
100int hflag; /* display in human readable format */
96
101
97int hasquota(struct fstab *, int, char **);
98int oneof(char *, char *[], int);
102int oneof(char *, char *[], int);
99int repquota(struct fstab *, int, char *);
103int repquota(struct fstab *, int);
100char *timeprt(time_t);
104char *timeprt(time_t);
105static void prthumanval(int64_t bytes);
101static void usage(void);
102
103int
104main(int argc, char *argv[])
105{
106 struct fstab *fs;
107 struct passwd *pw;
108 struct group *gr;
109 int ch, gflag = 0, uflag = 0, errs = 0;
110 long i, argnum, done = 0;
106static void usage(void);
107
108int
109main(int argc, char *argv[])
110{
111 struct fstab *fs;
112 struct passwd *pw;
113 struct group *gr;
114 int ch, gflag = 0, uflag = 0, errs = 0;
115 long i, argnum, done = 0;
111 char *qfnp;
112
116
113 while ((ch = getopt(argc, argv, "agnuv")) != -1) {
117 while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
114 switch(ch) {
115 case 'a':
116 aflag++;
117 break;
118 case 'g':
119 gflag++;
120 break;
118 switch(ch) {
119 case 'a':
120 aflag++;
121 break;
122 case 'g':
123 gflag++;
124 break;
125 case 'h':
126 hflag++;
127 break;
121 case 'n':
122 nflag++;
123 break;
124 case 'u':
125 uflag++;
126 break;
127 case 'v':
128 vflag++;

--- 23 unchanged lines hidden (view full) ---

152 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
153 endpwent();
154 }
155 setfsent();
156 while ((fs = getfsent()) != NULL) {
157 if (strcmp(fs->fs_vfstype, "ufs"))
158 continue;
159 if (aflag) {
128 case 'n':
129 nflag++;
130 break;
131 case 'u':
132 uflag++;
133 break;
134 case 'v':
135 vflag++;

--- 23 unchanged lines hidden (view full) ---

159 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
160 endpwent();
161 }
162 setfsent();
163 while ((fs = getfsent()) != NULL) {
164 if (strcmp(fs->fs_vfstype, "ufs"))
165 continue;
166 if (aflag) {
160 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
161 errs += repquota(fs, GRPQUOTA, qfnp);
162 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
163 errs += repquota(fs, USRQUOTA, qfnp);
167 if (gflag)
168 errs += repquota(fs, GRPQUOTA);
169 if (uflag)
170 errs += repquota(fs, USRQUOTA);
164 continue;
165 }
166 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
167 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
168 done |= 1 << argnum;
171 continue;
172 }
173 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
174 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
175 done |= 1 << argnum;
169 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
170 errs += repquota(fs, GRPQUOTA, qfnp);
171 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
172 errs += repquota(fs, USRQUOTA, qfnp);
176 if (gflag)
177 errs += repquota(fs, GRPQUOTA);
178 if (uflag)
179 errs += repquota(fs, USRQUOTA);
173 }
174 }
175 endfsent();
176 for (i = 0; i < argc; i++)
177 if ((done & (1 << i)) == 0)
178 warnx("%s not found in fstab", argv[i]);
179 exit(errs);
180}
181
182static void
183usage(void)
184{
185 fprintf(stderr, "%s\n%s\n",
180 }
181 }
182 endfsent();
183 for (i = 0; i < argc; i++)
184 if ((done & (1 << i)) == 0)
185 warnx("%s not found in fstab", argv[i]);
186 exit(errs);
187}
188
189static void
190usage(void)
191{
192 fprintf(stderr, "%s\n%s\n",
186 "usage: repquota [-v] [-g] [-n] [-u] -a",
187 " repquota [-v] [-g] [-n] [-u] filesystem ...");
193 "usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
194 " repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
188 exit(1);
189}
190
191int
195 exit(1);
196}
197
198int
192repquota(struct fstab *fs, int type, char *qfpathname)
199repquota(struct fstab *fs, int type)
193{
194 struct fileusage *fup;
200{
201 struct fileusage *fup;
195 FILE *qf;
196 u_long id;
202 struct quotafile *qf;
203 u_long id, maxid;
197 struct dqblk dqbuf;
204 struct dqblk dqbuf;
198 static struct dqblk zerodqblk;
199 static int warned = 0;
200 static int multiple = 0;
201
205 static int multiple = 0;
206
202 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
203 errno == EOPNOTSUPP && !warned && vflag) {
204 warned++;
205 fprintf(stdout,
206 "*** Warning: Quotas are not compiled into this kernel\n");
207 if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
208 if (vflag && !aflag) {
209 if (multiple++)
210 printf("\n");
211 fprintf(stdout, "*** No %s quotas on %s (%s)\n",
212 qfextension[type], fs->fs_file, fs->fs_spec);
213 return(1);
214 }
215 return(0);
207 }
208 if (multiple++)
209 printf("\n");
210 if (vflag)
211 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
212 qfextension[type], fs->fs_file, fs->fs_spec);
216 }
217 if (multiple++)
218 printf("\n");
219 if (vflag)
220 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
221 qfextension[type], fs->fs_file, fs->fs_spec);
213 if ((qf = fopen(qfpathname, "r")) == NULL) {
214 warn("%s", qfpathname);
215 return (1);
216 }
217 for (id = 0; ; id++) {
218 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
219 if (feof(qf))
222 printf("%*s Block limits File limits\n",
223 max(MAXLOGNAME - 1, 10), " ");
224 printf("User%*s used soft hard grace used soft hard grace\n",
225 max(MAXLOGNAME - 1, 10), " ");
226 maxid = quota_maxid(qf);
227 for (id = 0; id <= maxid; id++) {
228 if (quota_read(qf, &dqbuf, id) != 0)
220 break;
221 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
222 continue;
223 if ((fup = lookup(id, type)) == 0)
224 fup = addid(id, type, (char *)0);
229 break;
230 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
231 continue;
232 if ((fup = lookup(id, type)) == 0)
233 fup = addid(id, type, (char *)0);
225 fup->fu_dqblk = dqbuf;
234 printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
235 printf("%c%c",
236 dqbuf.dqb_bsoftlimit &&
237 dqbuf.dqb_curblocks >=
238 dqbuf.dqb_bsoftlimit ? '+' : '-',
239 dqbuf.dqb_isoftlimit &&
240 dqbuf.dqb_curinodes >=
241 dqbuf.dqb_isoftlimit ? '+' : '-');
242 prthumanval(dqbuf.dqb_curblocks);
243 prthumanval(dqbuf.dqb_bsoftlimit);
244 prthumanval(dqbuf.dqb_bhardlimit);
245 printf(" %6s",
246 dqbuf.dqb_bsoftlimit &&
247 dqbuf.dqb_curblocks >=
248 dqbuf.dqb_bsoftlimit ?
249 timeprt(dqbuf.dqb_btime) : "-");
250 printf(" %7ju %7ju %7ju %6s\n",
251 (uintmax_t)dqbuf.dqb_curinodes,
252 (uintmax_t)dqbuf.dqb_isoftlimit,
253 (uintmax_t)dqbuf.dqb_ihardlimit,
254 dqbuf.dqb_isoftlimit &&
255 dqbuf.dqb_curinodes >=
256 dqbuf.dqb_isoftlimit ?
257 timeprt(dqbuf.dqb_itime) : "-");
226 }
258 }
227 fclose(qf);
228 printf("%*s Block limits File limits\n",
229 max(MAXLOGNAME-1,10), " ");
230 printf("%s%*s used soft hard grace used soft hard grace\n",
231 type == USRQUOTA ? "User " : "Group", max(MAXLOGNAME-1,10), " ");
232 for (id = 0; id <= highid[type]; id++) {
233 fup = lookup(id, type);
234 if (fup == 0)
235 continue;
236 if (fup->fu_dqblk.dqb_curinodes == 0 &&
237 fup->fu_dqblk.dqb_curblocks == 0)
238 continue;
239 printf("%-*s ", max(MAXLOGNAME-1,10), fup->fu_name);
240 printf("%c%c %8lu %8lu %8lu %6s",
241 fup->fu_dqblk.dqb_bsoftlimit &&
242 fup->fu_dqblk.dqb_curblocks >=
243 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
244 fup->fu_dqblk.dqb_isoftlimit &&
245 fup->fu_dqblk.dqb_curinodes >=
246 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
247 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
248 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
249 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
250 fup->fu_dqblk.dqb_bsoftlimit &&
251 fup->fu_dqblk.dqb_curblocks >=
252 fup->fu_dqblk.dqb_bsoftlimit ?
253 timeprt(fup->fu_dqblk.dqb_btime) : "-");
254 printf(" %7lu %7lu %7lu %6s\n",
255 (u_long)fup->fu_dqblk.dqb_curinodes,
256 (u_long)fup->fu_dqblk.dqb_isoftlimit,
257 (u_long)fup->fu_dqblk.dqb_ihardlimit,
258 fup->fu_dqblk.dqb_isoftlimit &&
259 fup->fu_dqblk.dqb_curinodes >=
260 fup->fu_dqblk.dqb_isoftlimit ?
261 timeprt(fup->fu_dqblk.dqb_itime) : "-");
262 fup->fu_dqblk = zerodqblk;
263 }
259 quota_close(qf);
264 return (0);
265}
266
260 return (0);
261}
262
263static void
264prthumanval(int64_t blocks)
265{
266 char buf[7];
267 int flags;
268
269 if (!hflag) {
270 printf(" %6ju", (uintmax_t)dbtokb(blocks));
271 return;
272 }
273 flags = HN_NOSPACE | HN_DECIMAL;
274 if (blocks != 0)
275 flags |= HN_B;
276 humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
277 dbtob(blocks), "", HN_AUTOSCALE, flags);
278 (void)printf("%7s", buf);
279}
280
267/*
268 * Check to see if target appears in list of size cnt.
269 */
270int
271oneof(char *target, char *list[], int cnt)
272{
273 int i;
274
275 for (i = 0; i < cnt; i++)
276 if (strcmp(target, list[i]) == 0)
277 return (i);
278 return (-1);
279}
280
281/*
281/*
282 * Check to see if target appears in list of size cnt.
283 */
284int
285oneof(char *target, char *list[], int cnt)
286{
287 int i;
288
289 for (i = 0; i < cnt; i++)
290 if (strcmp(target, list[i]) == 0)
291 return (i);
292 return (-1);
293}
294
295/*
282 * Check to see if a particular quota is to be enabled.
283 */
284int
285hasquota(struct fstab *fs, int type, char **qfnamep)
286{
287 char *opt;
288 char *cp;
289 struct statfs sfb;
290 static char initname, usrname[100], grpname[100];
291 static char buf[BUFSIZ];
292
293 if (!initname) {
294 (void)snprintf(usrname, sizeof(usrname), "%s%s",
295 qfextension[USRQUOTA], qfname);
296 (void)snprintf(grpname, sizeof(grpname), "%s%s",
297 qfextension[GRPQUOTA], qfname);
298 initname = 1;
299 }
300 strcpy(buf, fs->fs_mntops);
301 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
302 if ((cp = index(opt, '=')))
303 *cp++ = '\0';
304 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
305 break;
306 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
307 break;
308 }
309 if (!opt)
310 return (0);
311 if (cp)
312 *qfnamep = cp;
313 else {
314 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
315 qfname, qfextension[type]);
316 *qfnamep = buf;
317 }
318 if (statfs(fs->fs_file, &sfb) != 0) {
319 warn("cannot statfs mount point %s", fs->fs_file);
320 return (0);
321 }
322 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
323 warnx("%s not mounted for %s quotas", fs->fs_file,
324 type == USRQUOTA ? "user" : "group");
325 return (0);
326 }
327 return (1);
328}
329
330/*
331 * Routines to manage the file usage table.
332 *
333 * Lookup an id of a specific type.
334 */
335struct fileusage *
336lookup(u_long id, int type)
337{
338 struct fileusage *fup;

--- 69 unchanged lines hidden ---
296 * Routines to manage the file usage table.
297 *
298 * Lookup an id of a specific type.
299 */
300struct fileusage *
301lookup(u_long id, int type)
302{
303 struct fileusage *fup;

--- 69 unchanged lines hidden ---