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 #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>
55 /* some flags of what to do: */
59 static void (*func)(int, struct fs *, char *);
60 static long blocksize;
64 static union dinode *get_inode(int, struct fs *, ino_t);
65 static int virtualblocks(struct fs *, union dinode *);
66 static int isfree(struct fs *, union dinode *);
67 static void inituser(void);
68 static void usrrehash(void);
69 static struct user *user(uid_t);
70 static int cmpusers(const void *, const void *);
71 static void uses(uid_t, daddr_t, time_t);
72 static void initfsizes(void);
73 static void dofsizes(int, struct fs *, char *);
74 static void douser(int, struct fs *, char *);
75 static void donames(int, struct fs *, char *);
76 static void usage(void);
77 static void quot(char *, char *);
80 * Original BSD quot doesn't round to number of frags/blocks,
81 * doesn't account for indirection blocks and gets it totally
82 * wrong if the size is a multiple of the blocksize.
83 * The new code always counts the number of 512 byte blocks
84 * instead of the number of kilobytes and converts them to
85 * kByte when done (on request).
87 * Due to the size of modern disks, we must cast intermediate
88 * values to 64 bits to prevent potential overflows.
93 #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
96 #define INOCNT(fs) ((fs)->fs_ipg)
98 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
99 sizeof(struct ufs2_dinode)) * INOCNT(fs))
102 struct ufs1_dinode dp1;
103 struct ufs2_dinode dp2;
105 #define DIP(fs, dp, field) \
106 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
107 (dp)->dp1.field : (dp)->dp2.field)
109 static union dinode *
110 get_inode(int fd, struct fs *super, ino_t ino)
112 static caddr_t ipbuf;
113 static struct cg *cgp;
116 struct ufs2_dinode *di2;
118 if (fd < 0) { /* flush cache */
122 if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) {
130 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
131 if (super->fs_magic == FS_UFS2_MAGIC &&
132 (!cgp || cg != ino_to_cg(super, ino))) {
133 cg = ino_to_cg(super, ino);
134 if (!cgp && !(cgp = malloc(super->fs_cgsize)))
135 errx(1, "allocate cg");
136 if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
138 if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
140 if (!cg_chkmagic(cgp))
141 errx(1, "cg has bad magic");
144 && !(ipbuf = malloc(INOSZ(super))))
145 errx(1, "allocate inodes");
146 last = rounddown(ino, INOCNT(super));
147 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
148 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
149 err(1, "read inodes");
152 if (super->fs_magic == FS_UFS1_MAGIC)
153 return ((union dinode *)
154 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
155 di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
156 /* If the inode is unused, it might be unallocated too, so zero it. */
157 if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
158 bzero(di2, sizeof (*di2));
159 return ((union dinode *)di2);
163 #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
165 #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
168 static int virtualblocks(struct fs *super, union dinode *dp)
172 sz = DIP(super, dp, di_size);
174 if (lblkno(super,sz) >= UFS_NDADDR) {
175 nblk = blkroundup(super,sz);
177 nblk += super->fs_bsize;
184 if (lblkno(super,sz) >= UFS_NDADDR) {
185 nblk = blkroundup(super,sz);
186 sz = lblkno(super,nblk);
187 sz = (sz - UFS_NDADDR + NINDIR(super) - 1) / NINDIR(super);
189 nblk += sz * super->fs_bsize;
190 /* sz - 1 rounded up */
191 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
194 nblk = fragroundup(super,sz);
201 isfree(struct fs *super, union dinode *dp)
204 return (DIP(super, dp, di_mode) & IFMT) == 0;
207 switch (DIP(super, dp, di_mode) & IFMT) {
209 case IFLNK: /* should check FASTSYMLINK? */
220 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
245 (struct user *)calloc(nusers,sizeof(struct user))))
246 errx(1, "allocate users");
248 for (usr = users, i = nusers; --i >= 0; usr++) {
249 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
259 struct user *usr, *usrn;
264 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
265 errx(1, "allocate users");
266 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
267 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
270 usrn = users + nusers;
284 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
289 if (!(pwd = getpwuid(uid))) {
290 if ((usr->name = (char *)malloc(7)))
291 sprintf(usr->name,"#%d",uid);
293 if ((usr->name = (char *)
294 malloc(strlen(pwd->pw_name) + 1)))
295 strcpy(usr->name,pwd->pw_name);
298 errx(1, "allocate users");
302 } else if (usr->uid == uid)
306 usr = users + nusers;
313 cmpusers(const void *v1, const void *v2)
315 const struct user *u1, *u2;
316 u1 = (const struct user *)v1;
317 u2 = (const struct user *)v2;
319 return u2->space - u1->space;
322 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
326 uses(uid_t uid, daddr_t blks, time_t act)
338 if (today - act > 90L * 24L * 60L * 60L)
340 if (today - act > 60L * 24L * 60L * 60L)
342 if (today - act > 30L * 24L * 60L * 60L)
352 struct fsizes *fsz_next;
353 daddr_t fsz_first, fsz_last;
354 ino_t fsz_count[FSZCNT];
355 daddr_t fsz_sz[FSZCNT];
364 for (fp = fsizes; fp; fp = fp->fsz_next) {
365 for (i = FSZCNT; --i >= 0;) {
366 fp->fsz_count[i] = 0;
373 dofsizes(int fd, struct fs *super, char *name)
378 struct fsizes *fp, **fsp;
381 maxino = super->fs_ncg * super->fs_ipg - 1;
383 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
384 errx(1, "allocate fsize structure");
386 for (inode = 0; inode < maxino; inode++) {
388 if ((dp = get_inode(fd,super,inode))
390 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
391 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
393 && !isfree(super, dp)
396 sz = estimate ? virtualblocks(super, dp) :
397 actualblocks(super, dp);
400 fsizes->fsz_count[FSZCNT-1]++;
401 fsizes->fsz_sz[FSZCNT-1] += sz;
403 fsizes->fsz_count[sz]++;
404 fsizes->fsz_sz[sz] += sz;
408 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
409 if (ksz < fp->fsz_last)
412 if (!fp || ksz < fp->fsz_first) {
413 if (!(fp = (struct fsizes *)
414 malloc(sizeof(struct fsizes))))
415 errx(1, "allocate fsize structure");
418 fp->fsz_first = rounddown(ksz, FSZCNT);
419 fp->fsz_last = fp->fsz_first + FSZCNT;
420 for (i = FSZCNT; --i >= 0;) {
421 fp->fsz_count[i] = 0;
425 fp->fsz_count[ksz % FSZCNT]++;
426 fp->fsz_sz[ksz % FSZCNT] += sz;
433 for (fp = fsizes; fp; fp = fp->fsz_next) {
434 for (i = 0; i < FSZCNT; i++) {
435 if (fp->fsz_count[i])
436 printf("%jd\t%jd\t%d\n",
437 (intmax_t)(fp->fsz_first + i),
438 (intmax_t)fp->fsz_count[i],
439 SIZE(sz += fp->fsz_sz[i]));
445 douser(int fd, struct fs *super, char *name)
448 struct user *usr, *usrs;
452 maxino = super->fs_ncg * super->fs_ipg - 1;
453 for (inode = 0; inode < maxino; inode++) {
455 if ((dp = get_inode(fd,super,inode))
456 && !isfree(super, dp))
457 uses(DIP(super, dp, di_uid),
458 estimate ? virtualblocks(super, dp) :
459 actualblocks(super, dp),
460 DIP(super, dp, di_atime));
465 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
466 errx(1, "allocate users");
467 bcopy(users,usrs,nusers * sizeof(struct user));
469 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
470 printf("%5d",SIZE(usr->space));
472 printf("\t%5ld",usr->count);
473 printf("\t%-8s",usr->name);
475 printf("\t%5d\t%5d\t%5d",
485 donames(int fd, struct fs *super, char *name)
492 maxino = super->fs_ncg * super->fs_ipg - 1;
493 /* first skip the name of the filesystem */
494 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
495 while ((c = getchar()) != EOF && c != '\n');
497 while (scanf("%ju", &inode) == 1) {
498 if (inode > maxino) {
499 warnx("illegal inode %ju", inode);
503 if ((dp = get_inode(fd,super,inode))
504 && !isfree(super, dp)) {
505 printf("%s\t",user(DIP(super, dp, di_uid))->name);
506 /* now skip whitespace */
507 while ((c = getchar()) == ' ' || c == '\t');
508 /* and print out the remainder of the input line */
509 while (c != EOF && c != '\n') {
519 while ((c = getchar()) != EOF && c != '\n');
530 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
532 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
538 quot(char *name, char *mp)
543 get_inode(-1, NULL, 0); /* flush cache */
546 if ((fd = open(name,0)) < 0) {
551 switch (errno = sbget(fd, &fs, UFS_STDSB, UFS_NOCSUM)) {
555 warn("Cannot find file system superblock");
559 warn("Unable to read file system superblock");
567 (*func)(fd, fs, name);
573 main(int argc, char *argv[])
582 header = getbsize(&headerlen,&blocksize);
584 while (--argc > 0 && **++argv == '-') {
616 cnt = getmntinfo(&mp,MNT_NOWAIT);
617 for (; --cnt >= 0; mp++) {
618 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN))
619 quot(mp->f_mntfromname, mp->f_mntonname);
622 while (--argc >= 0) {
623 if ((fs = getfsfile(*argv)) != NULL)
624 quot(fs->fs_spec, 0);