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>
40 #include <ufs/ufs/dinode.h>
41 #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(fd,super,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 = (ino / INOCNT(super)) * 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(super, dp)
174 register off_t nblk, sz;
176 sz = DIP(super, dp, di_size);
178 if (lblkno(super,sz) >= NDADDR) {
179 nblk = blkroundup(super,sz);
181 nblk += super->fs_bsize;
188 if (lblkno(super,sz) >= NDADDR) {
189 nblk = blkroundup(super,sz);
190 sz = lblkno(super,nblk);
191 sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
193 nblk += sz * super->fs_bsize;
194 /* sz - 1 rounded up */
195 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
198 nblk = fragroundup(super,sz);
210 return (DIP(super, dp, di_mode) & IFMT) == 0;
213 switch (DIP(super, dp, di_mode) & IFMT) {
215 case IFLNK: /* should check FASTSYMLINK? */
226 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
246 register struct user *usr;
251 (struct user *)calloc(nusers,sizeof(struct user))))
252 errx(1, "allocate users");
254 for (usr = users, i = nusers; --i >= 0; usr++) {
255 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
265 register struct user *usr, *usrn;
270 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
271 errx(1, "allocate users");
272 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
273 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
276 usrn = users + nusers;
286 register struct user *usr;
291 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
296 if (!(pwd = getpwuid(uid))) {
297 if ((usr->name = (char *)malloc(7)))
298 sprintf(usr->name,"#%d",uid);
300 if ((usr->name = (char *)
301 malloc(strlen(pwd->pw_name) + 1)))
302 strcpy(usr->name,pwd->pw_name);
305 errx(1, "allocate users");
309 } else if (usr->uid == uid)
313 usr = users + nusers;
323 const struct user *u1, *u2;
324 u1 = (const struct user *)v1;
325 u2 = (const struct user *)v2;
327 return u2->space - u1->space;
330 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
340 register struct user *usr;
349 if (today - act > 90L * 24L * 60L * 60L)
351 if (today - act > 60L * 24L * 60L * 60L)
353 if (today - act > 30L * 24L * 60L * 60L)
363 struct fsizes *fsz_next;
364 daddr_t fsz_first, fsz_last;
365 ino_t fsz_count[FSZCNT];
366 daddr_t fsz_sz[FSZCNT];
372 register struct fsizes *fp;
375 for (fp = fsizes; fp; fp = fp->fsz_next) {
376 for (i = FSZCNT; --i >= 0;) {
377 fp->fsz_count[i] = 0;
384 dofsizes(fd, super, name)
392 struct fsizes *fp, **fsp;
395 maxino = super->fs_ncg * super->fs_ipg - 1;
397 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
398 errx(1, "allocate fsize structure");
400 for (inode = 0; inode < maxino; inode++) {
402 if ((dp = get_inode(fd,super,inode))
404 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
405 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
407 && !isfree(super, dp)
410 sz = estimate ? virtualblocks(super, dp) :
411 actualblocks(super, dp);
414 fsizes->fsz_count[FSZCNT-1]++;
415 fsizes->fsz_sz[FSZCNT-1] += sz;
417 fsizes->fsz_count[sz]++;
418 fsizes->fsz_sz[sz] += sz;
422 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
423 if (ksz < fp->fsz_last)
426 if (!fp || ksz < fp->fsz_first) {
427 if (!(fp = (struct fsizes *)
428 malloc(sizeof(struct fsizes))))
429 errx(1, "allocate fsize structure");
432 fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
433 fp->fsz_last = fp->fsz_first + FSZCNT;
434 for (i = FSZCNT; --i >= 0;) {
435 fp->fsz_count[i] = 0;
439 fp->fsz_count[ksz % FSZCNT]++;
440 fp->fsz_sz[ksz % FSZCNT] += sz;
447 for (fp = fsizes; fp; fp = fp->fsz_next) {
448 for (i = 0; i < FSZCNT; i++) {
449 if (fp->fsz_count[i])
450 printf("%jd\t%jd\t%d\n",
451 (intmax_t)(fp->fsz_first + i),
452 (intmax_t)fp->fsz_count[i],
453 SIZE(sz += fp->fsz_sz[i]));
459 douser(fd, super, name)
465 struct user *usr, *usrs;
469 maxino = super->fs_ncg * super->fs_ipg - 1;
470 for (inode = 0; inode < maxino; inode++) {
472 if ((dp = get_inode(fd,super,inode))
473 && !isfree(super, dp))
474 uses(DIP(super, dp, di_uid),
475 estimate ? virtualblocks(super, dp) :
476 actualblocks(super, dp),
477 DIP(super, dp, di_atime));
482 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
483 errx(1, "allocate users");
484 bcopy(users,usrs,nusers * sizeof(struct user));
486 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
487 printf("%5d",SIZE(usr->space));
489 printf("\t%5ld",usr->count);
490 printf("\t%-8s",usr->name);
492 printf("\t%5d\t%5d\t%5d",
502 donames(fd, super, name)
512 maxino = super->fs_ncg * super->fs_ipg - 1;
513 /* first skip the name of the filesystem */
514 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
515 while ((c = getchar()) != EOF && c != '\n');
517 while (scanf("%u",&inode) == 1) {
518 if (inode > maxino) {
519 warnx("illegal inode %d",inode);
523 if ((dp = get_inode(fd,super,inode))
524 && !isfree(super, dp)) {
525 printf("%s\t",user(DIP(super, dp, di_uid))->name);
526 /* now skip whitespace */
527 while ((c = getchar()) == ' ' || c == '\t');
528 /* and print out the remainder of the input line */
529 while (c != EOF && c != '\n') {
539 while ((c = getchar()) != EOF && c != '\n');
550 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
552 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
558 * Possible superblock locations ordered from most to least likely.
560 static int sblock_try[] = SBLOCKSEARCH;
561 static char superblock[SBLOCKSIZE];
570 get_inode(-1, NULL, 0); /* flush cache */
573 if ((fd = open(name,0)) < 0) {
578 for (i = 0; sblock_try[i] != -1; i++) {
579 if (lseek(fd, sblock_try[i], 0) != sblock_try[i]) {
583 if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE) {
587 fs = (struct fs *)superblock;
588 if ((fs->fs_magic == FS_UFS1_MAGIC ||
589 (fs->fs_magic == FS_UFS2_MAGIC &&
590 fs->fs_sblockloc == sblock_try[i])) &&
591 fs->fs_bsize <= MAXBSIZE &&
592 fs->fs_bsize >= sizeof(struct fs))
595 if (sblock_try[i] == -1) {
596 warnx("%s: not a BSD filesystem",name);
604 (*func)(fd, fs, name);
616 char dev[MNAMELEN + 1];
622 header = getbsize(&headerlen,&blocksize);
624 while (--argc > 0 && **++argv == '-') {
656 cnt = getmntinfo(&mp,MNT_NOWAIT);
657 for (; --cnt >= 0; mp++) {
658 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN)) {
659 if ((nm = strrchr(mp->f_mntfromname,'/'))) {
660 sprintf(dev,"%s%s",_PATH_DEV,nm + 1);
663 nm = mp->f_mntfromname;
664 quot(nm,mp->f_mntonname);
668 while (--argc >= 0) {
669 if ((fs = getfsfile(*argv)) != NULL)
670 quot(fs->fs_spec, 0);