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>
57 /* some flags of what to do: */
61 static void (*func)(int, struct fs *, char *);
62 static long blocksize;
66 static union dinode *get_inode(int, struct fs *, ino_t);
67 static int virtualblocks(struct fs *, union dinode *);
68 static int isfree(struct fs *, union dinode *);
69 static void inituser(void);
70 static void usrrehash(void);
71 static struct user *user(uid_t);
72 static int cmpusers(const void *, const void *);
73 static void uses(uid_t, daddr_t, time_t);
74 static void initfsizes(void);
75 static void dofsizes(int, struct fs *, char *);
76 static void douser(int, struct fs *, char *);
77 static void donames(int, struct fs *, char *);
78 static void usage(void);
79 static void quot(char *, char *);
82 * Original BSD quot doesn't round to number of frags/blocks,
83 * doesn't account for indirection blocks and gets it totally
84 * wrong if the size is a multiple of the blocksize.
85 * The new code always counts the number of 512 byte blocks
86 * instead of the number of kilobytes and converts them to
87 * kByte when done (on request).
89 * Due to the size of modern disks, we must cast intermediate
90 * values to 64 bits to prevent potential overflows.
95 #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
98 #define INOCNT(fs) ((fs)->fs_ipg)
100 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
101 sizeof(struct ufs2_dinode)) * INOCNT(fs))
104 struct ufs1_dinode dp1;
105 struct ufs2_dinode dp2;
107 #define DIP(fs, dp, field) \
108 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
109 (dp)->dp1.field : (dp)->dp2.field)
111 static union dinode *
112 get_inode(int fd, struct fs *super, ino_t ino)
114 static caddr_t ipbuf;
115 static struct cg *cgp;
118 struct ufs2_dinode *di2;
120 if (fd < 0) { /* flush cache */
124 if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) {
132 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
133 if (super->fs_magic == FS_UFS2_MAGIC &&
134 (!cgp || cg != ino_to_cg(super, ino))) {
135 cg = ino_to_cg(super, ino);
136 if (!cgp && !(cgp = malloc(super->fs_cgsize)))
137 errx(1, "allocate cg");
138 if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
140 if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
142 if (!cg_chkmagic(cgp))
143 errx(1, "cg has bad magic");
146 && !(ipbuf = malloc(INOSZ(super))))
147 errx(1, "allocate inodes");
148 last = rounddown(ino, INOCNT(super));
149 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
150 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
151 err(1, "read inodes");
154 if (super->fs_magic == FS_UFS1_MAGIC)
155 return ((union dinode *)
156 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
157 di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
158 /* If the inode is unused, it might be unallocated too, so zero it. */
159 if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
160 bzero(di2, sizeof (*di2));
161 return ((union dinode *)di2);
165 #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
167 #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
170 static int virtualblocks(struct fs *super, union dinode *dp)
174 sz = DIP(super, dp, di_size);
176 if (lblkno(super,sz) >= UFS_NDADDR) {
177 nblk = blkroundup(super,sz);
179 nblk += super->fs_bsize;
186 if (lblkno(super,sz) >= UFS_NDADDR) {
187 nblk = blkroundup(super,sz);
188 sz = lblkno(super,nblk);
189 sz = (sz - UFS_NDADDR + NINDIR(super) - 1) / NINDIR(super);
191 nblk += sz * super->fs_bsize;
192 /* sz - 1 rounded up */
193 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
196 nblk = fragroundup(super,sz);
203 isfree(struct fs *super, union dinode *dp)
206 return (DIP(super, dp, di_mode) & IFMT) == 0;
209 switch (DIP(super, dp, di_mode) & IFMT) {
211 case IFLNK: /* should check FASTSYMLINK? */
222 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
247 (struct user *)calloc(nusers,sizeof(struct user))))
248 errx(1, "allocate users");
250 for (usr = users, i = nusers; --i >= 0; usr++) {
251 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
261 struct user *usr, *usrn;
266 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
267 errx(1, "allocate users");
268 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
269 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
272 usrn = users + nusers;
286 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
291 if (!(pwd = getpwuid(uid))) {
292 if ((usr->name = (char *)malloc(7)))
293 sprintf(usr->name,"#%d",uid);
295 if ((usr->name = (char *)
296 malloc(strlen(pwd->pw_name) + 1)))
297 strcpy(usr->name,pwd->pw_name);
300 errx(1, "allocate users");
304 } else if (usr->uid == uid)
308 usr = users + nusers;
315 cmpusers(const void *v1, const void *v2)
317 const struct user *u1, *u2;
318 u1 = (const struct user *)v1;
319 u2 = (const struct user *)v2;
321 return u2->space - u1->space;
324 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
328 uses(uid_t uid, daddr_t blks, time_t act)
340 if (today - act > 90L * 24L * 60L * 60L)
342 if (today - act > 60L * 24L * 60L * 60L)
344 if (today - act > 30L * 24L * 60L * 60L)
354 struct fsizes *fsz_next;
355 daddr_t fsz_first, fsz_last;
356 ino_t fsz_count[FSZCNT];
357 daddr_t fsz_sz[FSZCNT];
366 for (fp = fsizes; fp; fp = fp->fsz_next) {
367 for (i = FSZCNT; --i >= 0;) {
368 fp->fsz_count[i] = 0;
375 dofsizes(int fd, struct fs *super, char *name)
380 struct fsizes *fp, **fsp;
383 maxino = super->fs_ncg * super->fs_ipg - 1;
385 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
386 errx(1, "allocate fsize structure");
388 for (inode = 0; inode < maxino; inode++) {
390 if ((dp = get_inode(fd,super,inode))
392 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
393 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
395 && !isfree(super, dp)
398 sz = estimate ? virtualblocks(super, dp) :
399 actualblocks(super, dp);
402 fsizes->fsz_count[FSZCNT-1]++;
403 fsizes->fsz_sz[FSZCNT-1] += sz;
405 fsizes->fsz_count[sz]++;
406 fsizes->fsz_sz[sz] += sz;
410 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
411 if (ksz < fp->fsz_last)
414 if (!fp || ksz < fp->fsz_first) {
415 if (!(fp = (struct fsizes *)
416 malloc(sizeof(struct fsizes))))
417 errx(1, "allocate fsize structure");
420 fp->fsz_first = rounddown(ksz, FSZCNT);
421 fp->fsz_last = fp->fsz_first + FSZCNT;
422 for (i = FSZCNT; --i >= 0;) {
423 fp->fsz_count[i] = 0;
427 fp->fsz_count[ksz % FSZCNT]++;
428 fp->fsz_sz[ksz % FSZCNT] += sz;
435 for (fp = fsizes; fp; fp = fp->fsz_next) {
436 for (i = 0; i < FSZCNT; i++) {
437 if (fp->fsz_count[i])
438 printf("%jd\t%jd\t%d\n",
439 (intmax_t)(fp->fsz_first + i),
440 (intmax_t)fp->fsz_count[i],
441 SIZE(sz += fp->fsz_sz[i]));
447 douser(int fd, struct fs *super, char *name)
450 struct user *usr, *usrs;
454 maxino = super->fs_ncg * super->fs_ipg - 1;
455 for (inode = 0; inode < maxino; inode++) {
457 if ((dp = get_inode(fd,super,inode))
458 && !isfree(super, dp))
459 uses(DIP(super, dp, di_uid),
460 estimate ? virtualblocks(super, dp) :
461 actualblocks(super, dp),
462 DIP(super, dp, di_atime));
467 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
468 errx(1, "allocate users");
469 bcopy(users,usrs,nusers * sizeof(struct user));
471 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
472 printf("%5d",SIZE(usr->space));
474 printf("\t%5ld",usr->count);
475 printf("\t%-8s",usr->name);
477 printf("\t%5d\t%5d\t%5d",
487 donames(int fd, struct fs *super, char *name)
494 maxino = super->fs_ncg * super->fs_ipg - 1;
495 /* first skip the name of the filesystem */
496 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
497 while ((c = getchar()) != EOF && c != '\n');
499 while (scanf("%ju", &inode) == 1) {
500 if (inode > maxino) {
501 warnx("illegal inode %ju", inode);
505 if ((dp = get_inode(fd,super,inode))
506 && !isfree(super, dp)) {
507 printf("%s\t",user(DIP(super, dp, di_uid))->name);
508 /* now skip whitespace */
509 while ((c = getchar()) == ' ' || c == '\t');
510 /* and print out the remainder of the input line */
511 while (c != EOF && c != '\n') {
521 while ((c = getchar()) != EOF && c != '\n');
532 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
534 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
540 quot(char *name, char *mp)
545 get_inode(-1, NULL, 0); /* flush cache */
548 if ((fd = open(name,0)) < 0) {
553 switch (sbget(fd, &fs, STDSB)) {
557 warn("Cannot find file system superblock");
561 warn("Unable to read file system superblock");
569 (*func)(fd, fs, name);
577 main(int argc, char *argv[])
586 header = getbsize(&headerlen,&blocksize);
588 while (--argc > 0 && **++argv == '-') {
620 cnt = getmntinfo(&mp,MNT_NOWAIT);
621 for (; --cnt >= 0; mp++) {
622 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN))
623 quot(mp->f_mntfromname, mp->f_mntonname);
626 while (--argc >= 0) {
627 if ((fs = getfsfile(*argv)) != NULL)
628 quot(fs->fs_spec, 0);