2 * Copyright (C) 1991, 1994 Wolfgang Solfrank.
3 * Copyright (C) 1991, 1994 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/stdint.h>
37 #include <sys/mount.h>
38 #include <sys/disklabel.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ffs/fs.h>
54 /* some flags of what to do: */
58 static void (*func)(int, struct fs *, char *);
59 static long blocksize;
63 static union dinode *get_inode(int, struct fs *, ino_t);
64 static int virtualblocks(struct fs *, union dinode *);
65 static int isfree(struct fs *, union dinode *);
66 static void inituser(void);
67 static void usrrehash(void);
68 static struct user *user(uid_t);
69 static int cmpusers(const void *, const void *);
70 static void uses(uid_t, daddr_t, time_t);
71 static void initfsizes(void);
72 static void dofsizes(int, struct fs *, char *);
73 static void douser(int, struct fs *, char *);
74 static void donames(int, struct fs *, char *);
75 static void usage(void);
76 static void quot(char *, char *);
79 * Original BSD quot doesn't round to number of frags/blocks,
80 * doesn't account for indirection blocks and gets it totally
81 * wrong if the size is a multiple of the blocksize.
82 * The new code always counts the number of 512 byte blocks
83 * instead of the number of kilobytes and converts them to
84 * kByte when done (on request).
86 * Due to the size of modern disks, we must cast intermediate
87 * values to 64 bits to prevent potential overflows.
92 #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
95 #define INOCNT(fs) ((fs)->fs_ipg)
97 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
98 sizeof(struct ufs2_dinode)) * INOCNT(fs))
101 struct ufs1_dinode dp1;
102 struct ufs2_dinode dp2;
104 #define DIP(fs, dp, field) \
105 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
106 (dp)->dp1.field : (dp)->dp2.field)
108 static union dinode *
109 get_inode(int fd, struct fs *super, ino_t ino)
111 static caddr_t ipbuf;
112 static struct cg *cgp;
115 struct ufs2_dinode *di2;
117 if (fd < 0) { /* flush cache */
121 if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) {
129 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
130 if (super->fs_magic == FS_UFS2_MAGIC &&
131 (!cgp || cg != ino_to_cg(super, ino))) {
132 cg = ino_to_cg(super, ino);
133 if (!cgp && !(cgp = malloc(super->fs_cgsize)))
134 errx(1, "allocate cg");
135 if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
137 if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
139 if (!cg_chkmagic(cgp))
140 errx(1, "cg has bad magic");
143 && !(ipbuf = malloc(INOSZ(super))))
144 errx(1, "allocate inodes");
145 last = (ino / INOCNT(super)) * INOCNT(super);
146 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
147 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
148 err(1, "read inodes");
151 if (super->fs_magic == FS_UFS1_MAGIC)
152 return ((union dinode *)
153 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
154 di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
155 /* If the inode is unused, it might be unallocated too, so zero it. */
156 if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
157 bzero(di2, sizeof (*di2));
158 return ((union dinode *)di2);
162 #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
164 #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
167 static int virtualblocks(struct fs *super, union dinode *dp)
171 sz = DIP(super, dp, di_size);
173 if (lblkno(super,sz) >= NDADDR) {
174 nblk = blkroundup(super,sz);
176 nblk += super->fs_bsize;
183 if (lblkno(super,sz) >= NDADDR) {
184 nblk = blkroundup(super,sz);
185 sz = lblkno(super,nblk);
186 sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
188 nblk += sz * super->fs_bsize;
189 /* sz - 1 rounded up */
190 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
193 nblk = fragroundup(super,sz);
200 isfree(struct fs *super, union dinode *dp)
203 return (DIP(super, dp, di_mode) & IFMT) == 0;
206 switch (DIP(super, dp, di_mode) & IFMT) {
208 case IFLNK: /* should check FASTSYMLINK? */
219 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
244 (struct user *)calloc(nusers,sizeof(struct user))))
245 errx(1, "allocate users");
247 for (usr = users, i = nusers; --i >= 0; usr++) {
248 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
258 struct user *usr, *usrn;
263 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
264 errx(1, "allocate users");
265 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
266 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
269 usrn = users + nusers;
283 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
288 if (!(pwd = getpwuid(uid))) {
289 if ((usr->name = (char *)malloc(7)))
290 sprintf(usr->name,"#%d",uid);
292 if ((usr->name = (char *)
293 malloc(strlen(pwd->pw_name) + 1)))
294 strcpy(usr->name,pwd->pw_name);
297 errx(1, "allocate users");
301 } else if (usr->uid == uid)
305 usr = users + nusers;
312 cmpusers(const void *v1, const void *v2)
314 const struct user *u1, *u2;
315 u1 = (const struct user *)v1;
316 u2 = (const struct user *)v2;
318 return u2->space - u1->space;
321 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
325 uses(uid_t uid, daddr_t blks, time_t act)
337 if (today - act > 90L * 24L * 60L * 60L)
339 if (today - act > 60L * 24L * 60L * 60L)
341 if (today - act > 30L * 24L * 60L * 60L)
351 struct fsizes *fsz_next;
352 daddr_t fsz_first, fsz_last;
353 ino_t fsz_count[FSZCNT];
354 daddr_t fsz_sz[FSZCNT];
363 for (fp = fsizes; fp; fp = fp->fsz_next) {
364 for (i = FSZCNT; --i >= 0;) {
365 fp->fsz_count[i] = 0;
372 dofsizes(int fd, struct fs *super, char *name)
377 struct fsizes *fp, **fsp;
380 maxino = super->fs_ncg * super->fs_ipg - 1;
382 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
383 errx(1, "allocate fsize structure");
385 for (inode = 0; inode < maxino; inode++) {
387 if ((dp = get_inode(fd,super,inode))
389 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
390 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
392 && !isfree(super, dp)
395 sz = estimate ? virtualblocks(super, dp) :
396 actualblocks(super, dp);
399 fsizes->fsz_count[FSZCNT-1]++;
400 fsizes->fsz_sz[FSZCNT-1] += sz;
402 fsizes->fsz_count[sz]++;
403 fsizes->fsz_sz[sz] += sz;
407 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
408 if (ksz < fp->fsz_last)
411 if (!fp || ksz < fp->fsz_first) {
412 if (!(fp = (struct fsizes *)
413 malloc(sizeof(struct fsizes))))
414 errx(1, "allocate fsize structure");
417 fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
418 fp->fsz_last = fp->fsz_first + FSZCNT;
419 for (i = FSZCNT; --i >= 0;) {
420 fp->fsz_count[i] = 0;
424 fp->fsz_count[ksz % FSZCNT]++;
425 fp->fsz_sz[ksz % FSZCNT] += sz;
432 for (fp = fsizes; fp; fp = fp->fsz_next) {
433 for (i = 0; i < FSZCNT; i++) {
434 if (fp->fsz_count[i])
435 printf("%jd\t%jd\t%d\n",
436 (intmax_t)(fp->fsz_first + i),
437 (intmax_t)fp->fsz_count[i],
438 SIZE(sz += fp->fsz_sz[i]));
444 douser(int fd, struct fs *super, char *name)
447 struct user *usr, *usrs;
451 maxino = super->fs_ncg * super->fs_ipg - 1;
452 for (inode = 0; inode < maxino; inode++) {
454 if ((dp = get_inode(fd,super,inode))
455 && !isfree(super, dp))
456 uses(DIP(super, dp, di_uid),
457 estimate ? virtualblocks(super, dp) :
458 actualblocks(super, dp),
459 DIP(super, dp, di_atime));
464 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
465 errx(1, "allocate users");
466 bcopy(users,usrs,nusers * sizeof(struct user));
468 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
469 printf("%5d",SIZE(usr->space));
471 printf("\t%5ld",usr->count);
472 printf("\t%-8s",usr->name);
474 printf("\t%5d\t%5d\t%5d",
484 donames(int fd, struct fs *super, char *name)
491 maxino = super->fs_ncg * super->fs_ipg - 1;
492 /* first skip the name of the filesystem */
493 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
494 while ((c = getchar()) != EOF && c != '\n');
496 while (scanf("%u",&inode) == 1) {
497 if (inode > maxino) {
498 warnx("illegal inode %d",inode);
502 if ((dp = get_inode(fd,super,inode))
503 && !isfree(super, dp)) {
504 printf("%s\t",user(DIP(super, dp, di_uid))->name);
505 /* now skip whitespace */
506 while ((c = getchar()) == ' ' || c == '\t');
507 /* and print out the remainder of the input line */
508 while (c != EOF && c != '\n') {
518 while ((c = getchar()) != EOF && c != '\n');
529 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
531 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
537 * Possible superblock locations ordered from most to least likely.
539 static int sblock_try[] = SBLOCKSEARCH;
540 static char superblock[SBLOCKSIZE];
543 quot(char *name, char *mp)
548 get_inode(-1, NULL, 0); /* flush cache */
551 if ((fd = open(name,0)) < 0) {
556 for (i = 0; sblock_try[i] != -1; i++) {
557 if (lseek(fd, sblock_try[i], 0) != sblock_try[i]) {
561 if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE) {
565 fs = (struct fs *)superblock;
566 if ((fs->fs_magic == FS_UFS1_MAGIC ||
567 (fs->fs_magic == FS_UFS2_MAGIC &&
568 fs->fs_sblockloc == sblock_try[i])) &&
569 fs->fs_bsize <= MAXBSIZE &&
570 fs->fs_bsize >= sizeof(struct fs))
573 if (sblock_try[i] == -1) {
574 warnx("%s: not a BSD filesystem",name);
582 (*func)(fd, fs, name);
587 main(int argc, char *argv[])
596 header = getbsize(&headerlen,&blocksize);
598 while (--argc > 0 && **++argv == '-') {
630 cnt = getmntinfo(&mp,MNT_NOWAIT);
631 for (; --cnt >= 0; mp++) {
632 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN))
633 quot(mp->f_mntfromname, mp->f_mntonname);
636 while (--argc >= 0) {
637 if ((fs = getfsfile(*argv)) != NULL)
638 quot(fs->fs_spec, 0);