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.
33 static const char rcsid[] =
37 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <ufs/ffs/fs.h>
41 #include <ufs/ufs/quota.h>
42 #include <ufs/ufs/inode.h>
55 /* some flags of what to do: */
59 static void (*func)(int, struct fs *, char *);
60 static long blocksize;
64 static struct dinode *get_inode(int, struct fs *, ino_t);
65 static int virtualblocks(struct fs *, struct dinode *);
66 static int isfree(struct 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)
97 #define INOSZ(fs) (sizeof(struct dinode) * INOCNT(fs))
99 static struct dinode *
100 get_inode(fd,super,ino)
105 static struct dinode *ip;
108 if (fd < 0) { /* flush cache */
116 if (!ip || ino < last || ino >= last + INOCNT(super)) {
118 && !(ip = (struct dinode *)malloc(INOSZ(super))))
119 errx(1, "allocate inodes");
120 last = (ino / INOCNT(super)) * INOCNT(super);
121 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
122 || read(fd,ip,INOSZ(super)) != (ssize_t)INOSZ(super))
123 err(1, "read inodes");
126 return ip + ino % INOCNT(super);
130 #define actualblocks(super,ip) ((ip)->di_blocks/2)
132 #define actualblocks(super,ip) ((ip)->di_blocks)
135 static int virtualblocks(super,ip)
139 register off_t nblk, sz;
143 if (lblkno(super,sz) >= NDADDR) {
144 nblk = blkroundup(super,sz);
146 nblk += super->fs_bsize;
153 if (lblkno(super,sz) >= NDADDR) {
154 nblk = blkroundup(super,sz);
155 sz = lblkno(super,nblk);
156 sz = (sz - NDADDR + NINDIR(super) - 1) / NINDIR(super);
158 nblk += sz * super->fs_bsize;
159 /* sz - 1 rounded up */
160 sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
163 nblk = fragroundup(super,sz);
174 return (ip->di_mode&IFMT) == 0;
177 switch (ip->di_mode&IFMT) {
179 case IFLNK: /* should check FASTSYMLINK? */
204 register struct user *usr;
209 (struct user *)calloc(nusers,sizeof(struct user))))
210 errx(1, "allocate users");
212 for (usr = users, i = nusers; --i >= 0; usr++) {
213 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
223 register struct user *usr, *usrn;
228 if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
229 errx(1, "allocate users");
230 for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
231 for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
234 usrn = users + nusers;
244 register struct user *usr;
249 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
254 if (!(pwd = getpwuid(uid))) {
255 if ((usr->name = (char *)malloc(7)))
256 sprintf(usr->name,"#%d",uid);
258 if ((usr->name = (char *)
259 malloc(strlen(pwd->pw_name) + 1)))
260 strcpy(usr->name,pwd->pw_name);
263 errx(1, "allocate users");
267 } else if (usr->uid == uid)
271 usr = users + nusers;
281 const struct user *u1, *u2;
282 u1 = (const struct user *)v1;
283 u2 = (const struct user *)v2;
285 return u2->space - u1->space;
288 #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
298 register struct user *usr;
307 if (today - act > 90L * 24L * 60L * 60L)
309 if (today - act > 60L * 24L * 60L * 60L)
311 if (today - act > 30L * 24L * 60L * 60L)
321 struct fsizes *fsz_next;
322 daddr_t fsz_first, fsz_last;
323 ino_t fsz_count[FSZCNT];
324 daddr_t fsz_sz[FSZCNT];
330 register struct fsizes *fp;
333 for (fp = fsizes; fp; fp = fp->fsz_next) {
334 for (i = FSZCNT; --i >= 0;) {
335 fp->fsz_count[i] = 0;
342 dofsizes(fd,super,name)
350 struct fsizes *fp, **fsp;
353 maxino = super->fs_ncg * super->fs_ipg - 1;
355 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
356 errx(1, "allocate fsize structure");
358 for (inode = 0; inode < maxino; inode++) {
360 if ((ip = get_inode(fd,super,inode))
362 && ((ip->di_mode&IFMT) == IFREG
363 || (ip->di_mode&IFMT) == IFDIR)
368 sz = estimate ? virtualblocks(super,ip) :
369 actualblocks(super,ip);
372 fsizes->fsz_count[FSZCNT-1]++;
373 fsizes->fsz_sz[FSZCNT-1] += sz;
375 fsizes->fsz_count[sz]++;
376 fsizes->fsz_sz[sz] += sz;
380 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
381 if (ksz < fp->fsz_last)
384 if (!fp || ksz < fp->fsz_first) {
385 if (!(fp = (struct fsizes *)
386 malloc(sizeof(struct fsizes))))
387 errx(1, "allocate fsize structure");
390 fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
391 fp->fsz_last = fp->fsz_first + FSZCNT;
392 for (i = FSZCNT; --i >= 0;) {
393 fp->fsz_count[i] = 0;
397 fp->fsz_count[ksz % FSZCNT]++;
398 fp->fsz_sz[ksz % FSZCNT] += sz;
405 for (fp = fsizes; fp; fp = fp->fsz_next) {
406 for (i = 0; i < FSZCNT; i++) {
407 if (fp->fsz_count[i])
408 printf("%d\t%d\t%d\n",fp->fsz_first + i,
410 SIZE(sz += fp->fsz_sz[i]));
416 douser(fd,super,name)
422 struct user *usr, *usrs;
426 maxino = super->fs_ncg * super->fs_ipg - 1;
427 for (inode = 0; inode < maxino; inode++) {
429 if ((ip = get_inode(fd,super,inode))
432 estimate ? virtualblocks(super,ip) :
433 actualblocks(super,ip),
439 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
440 errx(1, "allocate users");
441 bcopy(users,usrs,nusers * sizeof(struct user));
443 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
444 printf("%5d",SIZE(usr->space));
446 printf("\t%5ld",usr->count);
447 printf("\t%-8s",usr->name);
449 printf("\t%5d\t%5d\t%5d",
459 donames(fd,super,name)
469 maxino = super->fs_ncg * super->fs_ipg - 1;
470 /* first skip the name of the filesystem */
471 while ((c = getchar()) != EOF && (c < '0' || c > '9'))
472 while ((c = getchar()) != EOF && c != '\n');
475 while (scanf("%u",&inode) == 1) {
476 if (inode > maxino) {
477 warnx("illegal inode %d",inode);
481 if ((ip = get_inode(fd,super,inode))
483 printf("%s\t",user(ip->di_uid)->name);
484 /* now skip whitespace */
485 while ((c = getchar()) == ' ' || c == '\t');
486 /* and print out the remainder of the input line */
487 while (c != EOF && c != '\n') {
498 while ((c = getchar()) != EOF && c != '\n');
509 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
511 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
516 static char superblock[SBSIZE];
524 get_inode(-1, NULL, 0); /* flush cache */
527 if ((fd = open(name,0)) < 0
528 || lseek(fd,SBOFF,0) != SBOFF
529 || read(fd,superblock,SBSIZE) != SBSIZE) {
534 if (((struct fs *)superblock)->fs_magic != FS_MAGIC) {
535 warnx("%s: not a BSD filesystem",name);
543 (*func)(fd,(struct fs *)superblock,name);
555 char dev[MNAMELEN + 1];
561 header = getbsize(&headerlen,&blocksize);
563 while (--argc > 0 && **++argv == '-') {
595 cnt = getmntinfo(&mp,MNT_NOWAIT);
596 for (; --cnt >= 0; mp++) {
597 if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN)) {
598 if ((nm = strrchr(mp->f_mntfromname,'/'))) {
599 sprintf(dev,"%s%s",_PATH_DEV,nm + 1);
602 nm = mp->f_mntfromname;
603 quot(nm,mp->f_mntonname);
607 while (--argc >= 0) {
608 if ((fs = getfsfile(*argv)) != NULL)
609 quot(fs->fs_spec, 0);