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;
117 if (fd < 0) { /* flush cache */
125 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
127 && !(ipbuf = malloc(INOSZ(super))))
128 errx(1, "allocate inodes");
129 last = (ino / INOCNT(super)) * INOCNT(super);
130 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
131 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
132 err(1, "read inodes");
135 if (super->fs_magic == FS_UFS1_MAGIC)
136 return ((union dinode *)
137 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
138 return ((union dinode *)
139 &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]);
143 #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
145 #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
148 static int virtualblocks(super, dp)
152 register off_t nblk, sz;
154 sz = DIP(super, dp, di_size);
156 if (lblkno(super,sz) >= NDADDR) {
157 nblk = blkroundup(super,sz);
159 nblk += super->fs_bsize;
166 if (lblkno(super,sz) >= NDADDR) {
167 nblk = blkroundup(super,sz);
168 sz = lblkno(super,nblk);
169 sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
171 nblk += sz * super->fs_bsize;
172 /* sz - 1 rounded up */
173 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
176 nblk = fragroundup(super,sz);
188 return (DIP(super, dp, di_mode) & IFMT) == 0;
191 switch (DIP(super, dp, di_mode) & IFMT) {
193 case IFLNK: /* should check FASTSYMLINK? */
218 register struct user *usr;
223 (struct user *)calloc(nusers,sizeof(struct user))))
224 errx(1, "allocate users");
226 for (usr = users, i = nusers; --i >= 0; usr++) {
227 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
237 register struct user *usr, *usrn;
242 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
243 errx(1, "allocate users");
244 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
245 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
248 usrn = users + nusers;
258 register struct user *usr;
263 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
268 if (!(pwd = getpwuid(uid))) {
269 if ((usr->name = (char *)malloc(7)))
270 sprintf(usr->name,"#%d",uid);
272 if ((usr->name = (char *)
273 malloc(strlen(pwd->pw_name) + 1)))
274 strcpy(usr->name,pwd->pw_name);
277 errx(1, "allocate users");
281 } else if (usr->uid == uid)
285 usr = users + nusers;
295 const struct user *u1, *u2;
296 u1 = (const struct user *)v1;
297 u2 = (const struct user *)v2;
299 return u2->space - u1->space;
302 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
312 register struct user *usr;
321 if (today - act > 90L * 24L * 60L * 60L)
323 if (today - act > 60L * 24L * 60L * 60L)
325 if (today - act > 30L * 24L * 60L * 60L)
335 struct fsizes *fsz_next;
336 daddr_t fsz_first, fsz_last;
337 ino_t fsz_count[FSZCNT];
338 daddr_t fsz_sz[FSZCNT];
344 register struct fsizes *fp;
347 for (fp = fsizes; fp; fp = fp->fsz_next) {
348 for (i = FSZCNT; --i >= 0;) {
349 fp->fsz_count[i] = 0;
356 dofsizes(fd, super, name)
364 struct fsizes *fp, **fsp;
367 maxino = super->fs_ncg * super->fs_ipg - 1;
369 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
370 errx(1, "allocate fsize structure");
372 for (inode = 0; inode < maxino; inode++) {
374 if ((dp = get_inode(fd,super,inode))
376 && ((DIP(super, dp, di_mode) & IFMT) == IFREG
377 || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
379 && !isfree(super, dp)
382 sz = estimate ? virtualblocks(super, dp) :
383 actualblocks(super, dp);
386 fsizes->fsz_count[FSZCNT-1]++;
387 fsizes->fsz_sz[FSZCNT-1] += sz;
389 fsizes->fsz_count[sz]++;
390 fsizes->fsz_sz[sz] += sz;
394 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
395 if (ksz < fp->fsz_last)
398 if (!fp || ksz < fp->fsz_first) {
399 if (!(fp = (struct fsizes *)
400 malloc(sizeof(struct fsizes))))
401 errx(1, "allocate fsize structure");
404 fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
405 fp->fsz_last = fp->fsz_first + FSZCNT;
406 for (i = FSZCNT; --i >= 0;) {
407 fp->fsz_count[i] = 0;
411 fp->fsz_count[ksz % FSZCNT]++;
412 fp->fsz_sz[ksz % FSZCNT] += sz;
419 for (fp = fsizes; fp; fp = fp->fsz_next) {
420 for (i = 0; i < FSZCNT; i++) {
421 if (fp->fsz_count[i])
422 printf("%jd\t%jd\t%d\n",
423 (intmax_t)(fp->fsz_first + i),
424 (intmax_t)fp->fsz_count[i],
425 SIZE(sz += fp->fsz_sz[i]));
431 douser(fd, super, name)
437 struct user *usr, *usrs;
441 maxino = super->fs_ncg * super->fs_ipg - 1;
442 for (inode = 0; inode < maxino; inode++) {
444 if ((dp = get_inode(fd,super,inode))
445 && !isfree(super, dp))
446 uses(DIP(super, dp, di_uid),
447 estimate ? virtualblocks(super, dp) :
448 actualblocks(super, dp),
449 DIP(super, dp, di_atime));
454 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
455 errx(1, "allocate users");
456 bcopy(users,usrs,nusers * sizeof(struct user));
458 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
459 printf("%5d",SIZE(usr->space));
461 printf("\t%5ld",usr->count);
462 printf("\t%-8s",usr->name);
464 printf("\t%5d\t%5d\t%5d",
474 donames(fd, super, name)
484 maxino = super->fs_ncg * super->fs_ipg - 1;
485 /* first skip the name of the filesystem */
486 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
487 while ((c = getchar()) != EOF && c != '\n');
489 while (scanf("%u",&inode) == 1) {
490 if (inode > maxino) {
491 warnx("illegal inode %d",inode);
495 if ((dp = get_inode(fd,super,inode))
496 && !isfree(super, dp)) {
497 printf("%s\t",user(DIP(super, dp, di_uid))->name);
498 /* now skip whitespace */
499 while ((c = getchar()) == ' ' || c == '\t');
500 /* and print out the remainder of the input line */
501 while (c != EOF && c != '\n') {
511 while ((c = getchar()) != EOF && c != '\n');
522 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
524 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
530 * Possible superblock locations ordered from most to least likely.
532 static int sblock_try[] = SBLOCKSEARCH;
533 static char superblock[SBLOCKSIZE];
542 get_inode(-1, NULL, 0); /* flush cache */
545 if ((fd = open(name,0)) < 0) {
550 for (i = 0; sblock_try[i] != -1; i++) {
551 if (lseek(fd, sblock_try[i], 0) != sblock_try[i]) {
555 if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE) {
559 fs = (struct fs *)superblock;
560 if ((fs->fs_magic == FS_UFS1_MAGIC ||
561 (fs->fs_magic == FS_UFS2_MAGIC &&
562 fs->fs_sblockloc == sblock_try[i])) &&
563 fs->fs_bsize <= MAXBSIZE &&
564 fs->fs_bsize >= sizeof(struct fs))
567 if (sblock_try[i] == -1) {
568 warnx("%s: not a BSD filesystem",name);
576 (*func)(fd, fs, name);
588 char dev[MNAMELEN + 1];
594 header = getbsize(&headerlen,&blocksize);
596 while (--argc > 0 && **++argv == '-') {
628 cnt = getmntinfo(&mp,MNT_NOWAIT);
629 for (; --cnt >= 0; mp++) {
630 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN)) {
631 if ((nm = strrchr(mp->f_mntfromname,'/'))) {
632 sprintf(dev,"%s%s",_PATH_DEV,nm + 1);
635 nm = mp->f_mntfromname;
636 quot(nm,mp->f_mntonname);
640 while (--argc >= 0) {
641 if ((fs = getfsfile(*argv)) != NULL)
642 quot(fs->fs_spec, 0);