2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1991, 1994 Wolfgang Solfrank.
5 * Copyright (C) 1991, 1994 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/stdint.h>
39 #include <sys/mount.h>
40 #include <sys/disklabel.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ffs/fs.h>
56 /* some flags of what to do: */
60 static void (*func)(int, struct fs *, char *);
61 static long blocksize;
65 static union dinode *get_inode(int, struct fs *, ino_t);
66 static int virtualblocks(struct fs *, union dinode *);
67 static int isfree(struct fs *, union dinode *);
68 static void inituser(void);
69 static void usrrehash(void);
70 static struct user *user(uid_t);
71 static int cmpusers(const void *, const void *);
72 static void uses(uid_t, daddr_t, time_t);
73 static void initfsizes(void);
74 static void dofsizes(int, struct fs *, char *);
75 static void douser(int, struct fs *, char *);
76 static void donames(int, struct fs *, char *);
77 static void usage(void);
78 static void quot(char *, char *);
81 * Original BSD quot doesn't round to number of frags/blocks,
82 * doesn't account for indirection blocks and gets it totally
83 * wrong if the size is a multiple of the blocksize.
84 * The new code always counts the number of 512 byte blocks
85 * instead of the number of kilobytes and converts them to
86 * kByte when done (on request).
88 * Due to the size of modern disks, we must cast intermediate
89 * values to 64 bits to prevent potential overflows.
94 #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
97 #define INOCNT(fs) ((fs)->fs_ipg)
99 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
100 sizeof(struct ufs2_dinode)) * INOCNT(fs))
103 struct ufs1_dinode dp1;
104 struct ufs2_dinode dp2;
106 #define DIP(fs, dp, field) \
107 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
108 (dp)->dp1.field : (dp)->dp2.field)
110 static union dinode *
111 get_inode(int fd, struct fs *super, ino_t ino)
113 static caddr_t ipbuf;
114 static struct cg *cgp;
117 struct ufs2_dinode *di2;
119 if (fd < 0) { /* flush cache */
123 if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) {
131 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
132 if (super->fs_magic == FS_UFS2_MAGIC &&
133 (!cgp || cg != ino_to_cg(super, ino))) {
134 cg = ino_to_cg(super, ino);
135 if (!cgp && !(cgp = malloc(super->fs_cgsize)))
136 errx(1, "allocate cg");
137 if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
139 if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
141 if (!cg_chkmagic(cgp))
142 errx(1, "cg has bad magic");
145 && !(ipbuf = malloc(INOSZ(super))))
146 errx(1, "allocate inodes");
147 last = rounddown(ino, INOCNT(super));
148 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
149 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
150 err(1, "read inodes");
153 if (super->fs_magic == FS_UFS1_MAGIC)
154 return ((union dinode *)
155 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
156 di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
157 /* If the inode is unused, it might be unallocated too, so zero it. */
158 if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
159 bzero(di2, sizeof (*di2));
160 return ((union dinode *)di2);
164 #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
166 #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
169 static int virtualblocks(struct fs *super, union dinode *dp)
173 sz = DIP(super, dp, di_size);
175 if (lblkno(super,sz) >= NDADDR) {
176 nblk = blkroundup(super,sz);
178 nblk += super->fs_bsize;
185 if (lblkno(super,sz) >= NDADDR) {
186 nblk = blkroundup(super,sz);
187 sz = lblkno(super,nblk);
188 sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
190 nblk += sz * super->fs_bsize;
191 /* sz - 1 rounded up */
192 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
195 nblk = fragroundup(super,sz);
202 isfree(struct fs *super, union dinode *dp)
205 return (DIP(super, dp, di_mode) & IFMT) == 0;
208 switch (DIP(super, dp, di_mode) & IFMT) {
210 case IFLNK: /* should check FASTSYMLINK? */
221 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
246 (struct user *)calloc(nusers,sizeof(struct user))))
247 errx(1, "allocate users");
249 for (usr = users, i = nusers; --i >= 0; usr++) {
250 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
260 struct user *usr, *usrn;
265 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
266 errx(1, "allocate users");
267 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
268 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
271 usrn = users + nusers;
285 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
290 if (!(pwd = getpwuid(uid))) {
291 if ((usr->name = (char *)malloc(7)))
292 sprintf(usr->name,"#%d",uid);
294 if ((usr->name = (char *)
295 malloc(strlen(pwd->pw_name) + 1)))
296 strcpy(usr->name,pwd->pw_name);
299 errx(1, "allocate users");
303 } else if (usr->uid == uid)
307 usr = users + nusers;
314 cmpusers(const void *v1, const void *v2)
316 const struct user *u1, *u2;
317 u1 = (const struct user *)v1;
318 u2 = (const struct user *)v2;
320 return u2->space - u1->space;
323 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
327 uses(uid_t uid, daddr_t blks, time_t act)
339 if (today - act > 90L * 24L * 60L * 60L)
341 if (today - act > 60L * 24L * 60L * 60L)
343 if (today - act > 30L * 24L * 60L * 60L)
353 struct fsizes *fsz_next;
354 daddr_t fsz_first, fsz_last;
355 ino_t fsz_count[FSZCNT];
356 daddr_t fsz_sz[FSZCNT];
365 for (fp = fsizes; fp; fp = fp->fsz_next) {
366 for (i = FSZCNT; --i >= 0;) {
367 fp->fsz_count[i] = 0;
374 dofsizes(int fd, struct fs *super, char *name)
379 struct fsizes *fp, **fsp;
382 maxino = super->fs_ncg * super->fs_ipg - 1;
384 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
385 errx(1, "allocate fsize structure");
387 for (inode = 0; inode < maxino; inode++) {
389 if ((dp = get_inode(fd,super,inode))
391 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
392 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
394 && !isfree(super, dp)
397 sz = estimate ? virtualblocks(super, dp) :
398 actualblocks(super, dp);
401 fsizes->fsz_count[FSZCNT-1]++;
402 fsizes->fsz_sz[FSZCNT-1] += sz;
404 fsizes->fsz_count[sz]++;
405 fsizes->fsz_sz[sz] += sz;
409 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
410 if (ksz < fp->fsz_last)
413 if (!fp || ksz < fp->fsz_first) {
414 if (!(fp = (struct fsizes *)
415 malloc(sizeof(struct fsizes))))
416 errx(1, "allocate fsize structure");
419 fp->fsz_first = rounddown(ksz, FSZCNT);
420 fp->fsz_last = fp->fsz_first + FSZCNT;
421 for (i = FSZCNT; --i >= 0;) {
422 fp->fsz_count[i] = 0;
426 fp->fsz_count[ksz % FSZCNT]++;
427 fp->fsz_sz[ksz % FSZCNT] += sz;
434 for (fp = fsizes; fp; fp = fp->fsz_next) {
435 for (i = 0; i < FSZCNT; i++) {
436 if (fp->fsz_count[i])
437 printf("%jd\t%jd\t%d\n",
438 (intmax_t)(fp->fsz_first + i),
439 (intmax_t)fp->fsz_count[i],
440 SIZE(sz += fp->fsz_sz[i]));
446 douser(int fd, struct fs *super, char *name)
449 struct user *usr, *usrs;
453 maxino = super->fs_ncg * super->fs_ipg - 1;
454 for (inode = 0; inode < maxino; inode++) {
456 if ((dp = get_inode(fd,super,inode))
457 && !isfree(super, dp))
458 uses(DIP(super, dp, di_uid),
459 estimate ? virtualblocks(super, dp) :
460 actualblocks(super, dp),
461 DIP(super, dp, di_atime));
466 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
467 errx(1, "allocate users");
468 bcopy(users,usrs,nusers * sizeof(struct user));
470 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
471 printf("%5d",SIZE(usr->space));
473 printf("\t%5ld",usr->count);
474 printf("\t%-8s",usr->name);
476 printf("\t%5d\t%5d\t%5d",
486 donames(int fd, struct fs *super, char *name)
493 maxino = super->fs_ncg * super->fs_ipg - 1;
494 /* first skip the name of the filesystem */
495 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
496 while ((c = getchar()) != EOF && c != '\n');
498 while (scanf("%ju", &inode) == 1) {
499 if (inode > maxino) {
500 warnx("illegal inode %ju", inode);
504 if ((dp = get_inode(fd,super,inode))
505 && !isfree(super, dp)) {
506 printf("%s\t",user(DIP(super, dp, di_uid))->name);
507 /* now skip whitespace */
508 while ((c = getchar()) == ' ' || c == '\t');
509 /* and print out the remainder of the input line */
510 while (c != EOF && c != '\n') {
520 while ((c = getchar()) != EOF && c != '\n');
531 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
533 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
539 * Possible superblock locations ordered from most to least likely.
541 static int sblock_try[] = SBLOCKSEARCH;
542 static char superblock[SBLOCKSIZE];
545 quot(char *name, char *mp)
550 get_inode(-1, NULL, 0); /* flush cache */
553 if ((fd = open(name,0)) < 0) {
558 for (i = 0; sblock_try[i] != -1; i++) {
559 if (lseek(fd, sblock_try[i], 0) != sblock_try[i]) {
563 if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE) {
567 fs = (struct fs *)superblock;
568 if ((fs->fs_magic == FS_UFS1_MAGIC ||
569 (fs->fs_magic == FS_UFS2_MAGIC &&
570 fs->fs_sblockloc == sblock_try[i])) &&
571 fs->fs_bsize <= MAXBSIZE &&
572 fs->fs_bsize >= sizeof(struct fs))
575 if (sblock_try[i] == -1) {
576 warnx("%s: not a BSD filesystem",name);
584 (*func)(fd, fs, name);
589 main(int argc, char *argv[])
598 header = getbsize(&headerlen,&blocksize);
600 while (--argc > 0 && **++argv == '-') {
632 cnt = getmntinfo(&mp,MNT_NOWAIT);
633 for (; --cnt >= 0; mp++) {
634 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN))
635 quot(mp->f_mntfromname, mp->f_mntonname);
638 while (--argc >= 0) {
639 if ((fs = getfsfile(*argv)) != NULL)
640 quot(fs->fs_spec, 0);