]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/repquota.tproj/repquota.c
2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1980, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Robert Elz at The University of Melbourne.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
62 __unused
static char copyright
[] =
63 "@(#) Copyright (c) 1980, 1990, 1993\n\
64 The Regents of the University of California. All rights reserved.\n";
68 __unused
static char sccsid
[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
74 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <sys/quota.h>
80 #include <sys/mount.h>
81 #endif /* __APPLE__ */
91 #include <libkern/OSByteOrder.h>
92 #endif /* __APPLE__ */
94 char *qfname
= QUOTAFILENAME
;
95 char *qfextension
[] = INITQFNAMES
;
97 u_int32_t quotamagic
[MAXQUOTAS
] = INITQMAGICS
;
98 #endif /* __APPLE__ */
102 struct fileusage
*fu_next
;
103 struct dqblk fu_dqblk
;
106 /* actually bigger */
108 struct fileusage
* addid(u_long
, int);
109 struct fileusage
* lookup(u_long
, int);
111 #define FUHASH 1024 /* must be power of two */
112 struct fileusage
*fuhead
[MAXQUOTAS
][FUHASH
];
113 u_long highid
[MAXQUOTAS
]; /* highest addid()'ed identifier per type */
114 #endif /* NOT __APPLE__ */
116 int vflag
; /* verbose */
117 int aflag
; /* all file systems */
119 int hasquota(struct statfs
*, int, char **);
120 int repquota(struct statfs
*, int, char *);
121 int oneof(char *, char **, int);
130 register struct fstab
*fs
;
131 register struct passwd
*pw
;
132 register struct group
*gr
;
133 #endif /* __APPLE__ */
134 int gflag
= 0, uflag
= 0, errs
= 0;
135 long i
, argnum
, done
= 0;
139 #endif /* __APPLE__ */
142 while ((ch
= getopt(argc
, argv
, "aguv")) != EOF
) {
162 if (argc
== 0 && !aflag
)
164 if (!gflag
&& !uflag
) {
171 nfst
= getmntinfo(&fst
, MNT_WAIT
);
173 fprintf(stderr
, "repquota: no filesystems mounted");
177 for (i
=0; i
<nfst
; i
++) {
178 if(strcmp(fst
[i
].f_fstypename
, "hfs")) {
182 if (gflag
&& hasquota(&fst
[i
], GRPQUOTA
, &qfnp
))
183 errs
+= repquota(&fst
[i
], GRPQUOTA
, qfnp
);
184 if (uflag
&& hasquota(&fst
[i
], USRQUOTA
, &qfnp
))
185 errs
+= repquota(&fst
[i
], USRQUOTA
, qfnp
);
188 if ((argnum
= oneof(fst
[i
].f_mntonname
, argv
, argc
)) >= 0 ||
189 (argnum
= oneof(fst
[i
].f_mntfromname
, argv
, argc
)) >= 0) {
191 if (gflag
&& hasquota(&fst
[i
], GRPQUOTA
, &qfnp
))
192 errs
+= repquota(&fst
[i
], GRPQUOTA
, qfnp
);
193 if (uflag
&& hasquota(&fst
[i
], USRQUOTA
, &qfnp
))
194 errs
+= repquota(&fst
[i
], USRQUOTA
, qfnp
);
200 while ((gr
= getgrent()) != 0)
201 (void) addid((u_long
)gr
->gr_gid
, GRPQUOTA
, gr
->gr_name
);
206 while ((pw
= getpwent()) != 0)
207 (void) addid((u_long
)pw
->pw_uid
, USRQUOTA
, pw
->pw_name
);
212 while ((fs
= getfsent()) != NULL
) {
213 if (strcmp(fs
->fs_vfstype
, "ufs"))
216 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
217 errs
+= repquota(fs
, GRPQUOTA
, qfnp
);
218 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
219 errs
+= repquota(fs
, USRQUOTA
, qfnp
);
222 if ((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
223 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) {
225 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
226 errs
+= repquota(fs
, GRPQUOTA
, qfnp
);
227 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
228 errs
+= repquota(fs
, USRQUOTA
, qfnp
);
233 for (i
= 0; i
< argc
; i
++)
234 if ((done
& (1 << i
)) == 0)
235 fprintf(stderr
, "%s not found in fstab\n", argv
[i
]);
242 fprintf(stderr
, "Usage:\n\t%s\n\t%s\n",
243 "repquota [-v] [-g] [-u] -a",
244 "repquota [-v] [-g] [-u] filesys ...");
250 repquota(fst
, type
, qfpathname
)
260 struct dqfilehdr dqhdr
;
261 static int warned
= 0;
262 static int multiple
= 0;
268 u_int64_t bsoftlimit
;
269 u_int32_t isoftlimit
;
274 if (quotactl(fst
->f_mntonname
, QCMD(Q_SYNC
, type
), 0, 0) < 0 &&
275 errno
== ENOTSUP
&& !warned
&& vflag
) {
278 "*** Warning: Quotas are not compiled into this kernel\n");
283 fprintf(stdout
, "*** Report for %s quotas on %s (%s)\n",
284 qfextension
[type
], fst
->f_mntonname
, fst
->f_mntfromname
);
285 if ((qf
= fopen(qfpathname
, "r")) == NULL
) {
290 /* Read in the quota file header. */
291 if (fread((char *)&dqhdr
, sizeof(struct dqfilehdr
), 1, qf
) > 0) {
292 /* Check for non big endian file. */
293 if (OSSwapBigToHostInt32(dqhdr
.dqh_magic
) != quotamagic
[type
] &&
294 OSSwapInt32(dqhdr
.dqh_magic
) == quotamagic
[type
]) {
295 (void) fprintf(stderr
,
296 "*** Error: %s: not in big endian byte order\n", qfpathname
);
300 /* Sanity check the quota file header. */
301 if ((OSSwapBigToHostInt32(dqhdr
.dqh_magic
) != quotamagic
[type
]) ||
302 (OSSwapBigToHostInt32(dqhdr
.dqh_version
) > QF_VERSION
) ||
303 (!powerof2(OSSwapBigToHostInt32(dqhdr
.dqh_maxentries
)))) {
304 (void) fprintf(stderr
,
305 "repquota: %s: not a valid quota file\n", qfpathname
);
311 printf(" 1K Block limits File limits\n");
312 printf("User used soft hard grace used soft hard grace\n");
314 maxentries
= OSSwapBigToHostInt32(dqhdr
.dqh_maxentries
);
316 /* Read the entries in the quota file. */
317 for (i
= 0; i
< maxentries
; i
++) {
318 if (fread(&dqbuf
, sizeof(struct dqblk
), 1, qf
) == 0 && feof(qf
))
320 id
= OSSwapBigToHostInt32(dqbuf
.dqb_id
);
323 if (dqbuf
.dqb_curinodes
== 0 && dqbuf
.dqb_curbytes
== 0LL)
328 if ((pw
= getpwuid(id
)) != 0)
332 if ((gr
= getgrgid(id
)) != 0)
337 printf("%-10s", name
);
339 printf("%-10u", (unsigned int)id
);
341 bsoftlimit
= OSSwapBigToHostInt64( dqbuf
.dqb_bsoftlimit
);
342 isoftlimit
= OSSwapBigToHostInt32( dqbuf
.dqb_isoftlimit
);
343 curbytes
= OSSwapBigToHostInt64( dqbuf
.dqb_curbytes
);
344 curinodes
= OSSwapBigToHostInt32( dqbuf
.dqb_curinodes
);
346 printf("%c%c%12qd%12qd%12qd%7s",
349 bsoftlimit
? '+' : '-',
352 isoftlimit
? '+' : '-',
355 OSSwapBigToHostInt64( dqbuf
.dqb_bhardlimit
) / 1024,
359 timeprt(OSSwapBigToHostInt32(dqbuf
.dqb_btime
)) : "");
360 printf(" %6d%6d%6d%7s\n",
363 OSSwapBigToHostInt32( dqbuf
.dqb_ihardlimit
),
367 timeprt(OSSwapBigToHostInt32(dqbuf
.dqb_itime
)) : "");
374 repquota(fs
, type
, qfpathname
)
375 register struct fstab
*fs
;
379 register struct fileusage
*fup
;
384 static struct dqblk zerodqblk
;
385 static int warned
= 0;
386 static int multiple
= 0;
389 if (quotactl(fs
->fs_file
, QCMD(Q_SYNC
, type
), 0, 0) < 0 &&
390 errno
== ENOTSUP
&& !warned
&& vflag
) {
393 "*** Warning: Quotas are not compiled into this kernel\n");
398 fprintf(stdout
, "*** Report for %s quotas on %s (%s)\n",
399 qfextension
[type
], fs
->fs_file
, fs
->fs_spec
);
400 if ((qf
= fopen(qfpathname
, "r")) == NULL
) {
404 for (id
= 0; ; id
++) {
405 fread(&dqbuf
, sizeof(struct dqblk
), 1, qf
);
408 if (dqbuf
.dqb_curinodes
== 0 && dqbuf
.dqb_curblocks
== 0)
410 if ((fup
= lookup(id
, type
)) == 0)
411 fup
= addid(id
, type
, (char *)0);
412 fup
->fu_dqblk
= dqbuf
;
415 printf(" Block limits File limits\n");
416 printf("User used soft hard grace used soft hard grace\n");
418 for (id
= 0; id
<= highid
[type
]; id
++) {
419 fup
= lookup(id
, type
);
422 if (fup
->fu_dqblk
.dqb_curinodes
== 0 &&
423 fup
->fu_dqblk
.dqb_curblocks
== 0)
425 printf("%-10s", fup
->fu_name
);
426 printf("%c%c%8d%8d%8d%7s",
427 fup
->fu_dqblk
.dqb_bsoftlimit
&&
428 fup
->fu_dqblk
.dqb_curblocks
>=
429 fup
->fu_dqblk
.dqb_bsoftlimit
? '+' : '-',
430 fup
->fu_dqblk
.dqb_isoftlimit
&&
431 fup
->fu_dqblk
.dqb_curinodes
>=
432 fup
->fu_dqblk
.dqb_isoftlimit
? '+' : '-',
433 dbtob(fup
->fu_dqblk
.dqb_curblocks
) / 1024,
434 dbtob(fup
->fu_dqblk
.dqb_bsoftlimit
) / 1024,
435 dbtob(fup
->fu_dqblk
.dqb_bhardlimit
) / 1024,
436 fup
->fu_dqblk
.dqb_bsoftlimit
&&
437 fup
->fu_dqblk
.dqb_curblocks
>=
438 fup
->fu_dqblk
.dqb_bsoftlimit
?
439 timeprt(fup
->fu_dqblk
.dqb_btime
) : "");
440 printf(" %6d%6d%6d%7s\n",
441 fup
->fu_dqblk
.dqb_curinodes
,
442 fup
->fu_dqblk
.dqb_isoftlimit
,
443 fup
->fu_dqblk
.dqb_ihardlimit
,
444 fup
->fu_dqblk
.dqb_isoftlimit
&&
445 fup
->fu_dqblk
.dqb_curinodes
>=
446 fup
->fu_dqblk
.dqb_isoftlimit
?
447 timeprt(fup
->fu_dqblk
.dqb_itime
) : "");
448 fup
->fu_dqblk
= zerodqblk
;
455 * Check to see if target appears in list of size cnt.
458 oneof(target
, list
, cnt
)
459 register char *target
, **list
;
464 for (i
= 0; i
< cnt
; i
++)
465 if (strcmp(target
, list
[i
]) == 0)
471 * Check to see if a particular quota is to be enabled.
475 hasquota(fst
, type
, qfnamep
)
476 register struct statfs
*fst
;
481 static char initname
, usrname
[100], grpname
[100];
482 static char buf
[BUFSIZ
];
485 snprintf(usrname
, sizeof(usrname
), "%s%s", qfextension
[USRQUOTA
], qfname
);
486 snprintf(grpname
, sizeof(grpname
), "%s%s", qfextension
[GRPQUOTA
], qfname
);
491 We only support the default path to the
495 (void)snprintf(buf
, sizeof(buf
), "%s/%s.%s", fst
->f_mntonname
,
496 QUOTAOPSNAME
, qfextension
[type
] );
497 if (stat(buf
, &sb
) != 0) {
498 /* There appears to be no mount option file */
502 (void) snprintf(buf
, sizeof(buf
), "%s/%s.%s", fst
->f_mntonname
, qfname
, qfextension
[type
]);
507 hasquota(fs
, type
, qfnamep
)
508 register struct fstab
*fs
;
513 char *cp
, *index(), *strtok();
514 static char initname
, usrname
[100], grpname
[100];
515 static char buf
[BUFSIZ
];
518 snprintf(usrname
, sizoef(usrname
), "%s%s", qfextension
[USRQUOTA
], qfname
);
519 snprintf(grpname
, sizeof(grpname
), "%s%s", qfextension
[GRPQUOTA
], qfname
);
522 strlcpy(buf
, fs
->fs_mntops
, sizeof(buf
));
523 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
524 if (cp
= index(opt
, '='))
526 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
528 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
537 (void) snprintf(buf
, sizeof(buf
), "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
541 #endif /* __APPLE__ */
547 * Routines to manage the file usage table.
549 * Lookup an id of a specific type.
556 register struct fileusage
*fup
;
558 for (fup
= fuhead
[type
][id
& (FUHASH
-1)]; fup
!= 0; fup
= fup
->fu_next
)
559 if (fup
->fu_id
== id
)
561 return ((struct fileusage
*)0);
565 * Add a new file usage id if it does not already exist.
568 addid(id
, type
, name
)
573 struct fileusage
*fup
, **fhp
;
575 extern char *calloc();
577 if (fup
= lookup(id
, type
))
583 if ((fup
= (struct fileusage
*)calloc(1, sizeof(*fup
) + len
)) == NULL
) {
584 fprintf(stderr
, "out of memory for fileusage structures\n");
587 fhp
= &fuhead
[type
][id
& (FUHASH
- 1)];
591 if (id
> highid
[type
])
594 bcopy(name
, fup
->fu_name
, len
+ 1);
596 snprintf(fup
->fu_name
, len
+ 1, "%u", id
);
600 #endif /* !__APPLE__ */
603 * Calculate the grace period and return a printable string for it.
609 time_t hours
, minutes
;
618 minutes
= (seconds
+ 30) / 60;
619 hours
= (minutes
+ 30) / 60;
621 snprintf(buf
, sizeof(buf
), "%lddays", (hours
+ 12) / 24);
625 snprintf(buf
, sizeof(buf
), "%2ld:%ld", minutes
/ 60, minutes
% 60);
628 snprintf(buf
, sizeof(buf
), "%2ld", minutes
);