Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / quot / quot.c
1 /*
2  * Copyright (C) 1991, 1994 Wolfgang Solfrank.
3  * Copyright (C) 1991, 1994 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
19  *
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.
30  */
31
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD: src/usr.sbin/quot/quot.c,v 1.11.2.4 2002/03/15 18:12:41 mikeh Exp $";
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/time.h>
40 #include <ufs/ffs/fs.h>
41 #include <ufs/ufs/quota.h>
42 #include <ufs/ufs/inode.h>
43
44 #include <err.h>
45 #include <fcntl.h>
46 #include <fstab.h>
47 #include <errno.h>
48 #include <paths.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 /* some flags of what to do: */
56 static char estimate;
57 static char count;
58 static char unused;
59 static void (*func)(int, struct fs *, char *);
60 static long blocksize;
61 static char *header;
62 static int headerlen;
63
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 *);
78
79 /*
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).
86  *
87  * Due to the size of modern disks, we must cast intermediate
88  * values to 64 bits to prevent potential overflows.
89  */
90 #ifdef  COMPAT
91 #define SIZE(n) (n)
92 #else
93 #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
94 #endif
95
96 #define INOCNT(fs)      ((fs)->fs_ipg)
97 #define INOSZ(fs)       (sizeof(struct dinode) * INOCNT(fs))
98
99 static struct dinode *
100 get_inode(fd,super,ino)
101         int fd;
102         struct fs *super;
103         ino_t ino;
104 {
105         static struct dinode *ip;
106         static ino_t last;
107         
108         if (fd < 0) {           /* flush cache */
109                 if (ip) {
110                         free(ip);
111                         ip = 0;
112                 }
113                 return 0;
114         }
115         
116         if (!ip || ino < last || ino >= last + INOCNT(super)) {
117                 if (!ip
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");
124         }
125         
126         return ip + ino % INOCNT(super);
127 }
128
129 #ifdef  COMPAT
130 #define actualblocks(super,ip)  ((ip)->di_blocks/2)
131 #else
132 #define actualblocks(super,ip)  ((ip)->di_blocks)
133 #endif
134
135 static int virtualblocks(super,ip)
136         struct fs *super;
137         struct dinode *ip;
138 {
139         register off_t nblk, sz;
140         
141         sz = ip->di_size;
142 #ifdef  COMPAT
143         if (lblkno(super,sz) >= NDADDR) {
144                 nblk = blkroundup(super,sz);
145                 if (sz == nblk)
146                         nblk += super->fs_bsize;
147         }
148         
149         return sz / 1024;
150         
151 #else   /* COMPAT */
152         
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);
157                 while (sz > 0) {
158                         nblk += sz * super->fs_bsize;
159                         /* sz - 1 rounded up */
160                         sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
161                 }
162         } else
163                 nblk = fragroundup(super,sz);
164         
165         return nblk / 512;
166 #endif  /* COMPAT */
167 }
168
169 static int
170 isfree(ip)
171         struct dinode *ip;
172 {
173 #ifdef  COMPAT
174         return (ip->di_mode&IFMT) == 0;
175 #else   /* COMPAT */
176         
177         switch (ip->di_mode&IFMT) {
178         case IFIFO:
179         case IFLNK:             /* should check FASTSYMLINK? */
180         case IFDIR:
181         case IFREG:
182                 return 0;
183         default:
184                 return 1;
185         }
186 #endif
187 }
188
189 static struct user {
190         uid_t uid;
191         char *name;
192         daddr_t space;
193         long count;
194         daddr_t spc30;
195         daddr_t spc60;
196         daddr_t spc90;
197 } *users;
198 static int nusers;
199
200 static void
201 inituser()
202 {
203         register int i;
204         register struct user *usr;
205         
206         if (!nusers) {
207                 nusers = 8;
208                 if (!(users =
209                     (struct user *)calloc(nusers,sizeof(struct user))))
210                         errx(1, "allocate users");
211         } else {
212                 for (usr = users, i = nusers; --i >= 0; usr++) {
213                         usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
214                         usr->count = 0;
215                 }
216         }
217 }
218
219 static void
220 usrrehash()
221 {
222         register int i;
223         register struct user *usr, *usrn;
224         struct user *svusr;
225         
226         svusr = users;
227         nusers <<= 1;
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;
232                     usrn--) {
233                         if (usrn <= users)
234                                 usrn = users + nusers;
235                 }
236                 *usrn = *usr;
237         }
238 }
239
240 static struct user *
241 user(uid)
242         uid_t uid;
243 {
244         register struct user *usr;
245         register int i;
246         struct passwd *pwd;
247         
248         while (1) {
249                 for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
250                     usr--) {
251                         if (!usr->name) {
252                                 usr->uid = uid;
253                                 
254                                 if (!(pwd = getpwuid(uid))) {
255                                         if ((usr->name = (char *)malloc(7)))
256                                                 sprintf(usr->name,"#%d",uid);
257                                 } else {
258                                         if ((usr->name = (char *)
259                                             malloc(strlen(pwd->pw_name) + 1)))
260                                                 strcpy(usr->name,pwd->pw_name);
261                                 }
262                                 if (!usr->name)
263                                         errx(1, "allocate users");
264                                 
265                                 return usr;
266                                 
267                         } else if (usr->uid == uid)
268                                 return usr;
269
270                         if (usr <= users)
271                                 usr = users + nusers;
272                 }
273                 usrrehash();
274         }
275 }
276
277 static int
278 cmpusers(v1,v2)
279         const void *v1, *v2;
280 {
281         const struct user *u1, *u2;
282         u1 = (const struct user *)v1;
283         u2 = (const struct user *)v2;
284
285         return u2->space - u1->space;
286 }
287
288 #define sortusers(users)        (qsort((users),nusers,sizeof(struct user), \
289                                     cmpusers))
290
291 static void
292 uses(uid,blks,act)
293         uid_t uid;
294         daddr_t blks;
295         time_t act;
296 {
297         static time_t today;
298         register struct user *usr;
299         
300         if (!today)
301                 time(&today);
302         
303         usr = user(uid);
304         usr->count++;
305         usr->space += blks;
306         
307         if (today - act > 90L * 24L * 60L * 60L)
308                 usr->spc90 += blks;
309         if (today - act > 60L * 24L * 60L * 60L)
310                 usr->spc60 += blks;
311         if (today - act > 30L * 24L * 60L * 60L)
312                 usr->spc30 += blks;
313 }
314
315 #ifdef  COMPAT
316 #define FSZCNT  500
317 #else
318 #define FSZCNT  512
319 #endif
320 struct fsizes {
321         struct fsizes *fsz_next;
322         daddr_t fsz_first, fsz_last;
323         ino_t fsz_count[FSZCNT];
324         daddr_t fsz_sz[FSZCNT];
325 } *fsizes;
326
327 static void
328 initfsizes()
329 {
330         register struct fsizes *fp;
331         register int i;
332         
333         for (fp = fsizes; fp; fp = fp->fsz_next) {
334                 for (i = FSZCNT; --i >= 0;) {
335                         fp->fsz_count[i] = 0;
336                         fp->fsz_sz[i] = 0;
337                 }
338         }
339 }
340
341 static void
342 dofsizes(fd,super,name)
343         int fd;
344         struct fs *super;
345         char *name;
346 {
347         ino_t inode, maxino;
348         struct dinode *ip;
349         daddr_t sz, ksz;
350         struct fsizes *fp, **fsp;
351         register int i;
352         
353         maxino = super->fs_ncg * super->fs_ipg - 1;
354 #ifdef  COMPAT
355         if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
356                 errx(1, "allocate fsize structure");
357 #endif  /* COMPAT */
358         for (inode = 0; inode < maxino; inode++) {
359                 errno = 0;
360                 if ((ip = get_inode(fd,super,inode))
361 #ifdef  COMPAT
362                     && ((ip->di_mode&IFMT) == IFREG
363                         || (ip->di_mode&IFMT) == IFDIR)
364 #else   /* COMPAT */
365                     && !isfree(ip)
366 #endif  /* COMPAT */
367                     ) {
368                         sz = estimate ? virtualblocks(super,ip) :
369                             actualblocks(super,ip);
370 #ifdef  COMPAT
371                         if (sz >= FSZCNT) {
372                                 fsizes->fsz_count[FSZCNT-1]++;
373                                 fsizes->fsz_sz[FSZCNT-1] += sz;
374                         } else {
375                                 fsizes->fsz_count[sz]++;
376                                 fsizes->fsz_sz[sz] += sz;
377                         }
378 #else   /* COMPAT */
379                         ksz = SIZE(sz);
380                         for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
381                                 if (ksz < fp->fsz_last)
382                                         break;
383                         }
384                         if (!fp || ksz < fp->fsz_first) {
385                                 if (!(fp = (struct fsizes *)
386                                     malloc(sizeof(struct fsizes))))
387                                         errx(1, "allocate fsize structure");
388                                 fp->fsz_next = *fsp;
389                                 *fsp = fp;
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;
394                                         fp->fsz_sz[i] = 0;
395                                 }
396                         }
397                         fp->fsz_count[ksz % FSZCNT]++;
398                         fp->fsz_sz[ksz % FSZCNT] += sz;
399 #endif  /* COMPAT */
400                 } else if (errno) {
401                         err(1, "%s", name);
402                 }
403         }
404         sz = 0;
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,
409                                     fp->fsz_count[i],
410                                     SIZE(sz += fp->fsz_sz[i]));
411                 }
412         }
413 }
414
415 static void
416 douser(fd,super,name)
417         int fd;
418         struct fs *super;
419         char *name;
420 {
421         ino_t inode, maxino;
422         struct user *usr, *usrs;
423         struct dinode *ip;
424         register int n;
425         
426         maxino = super->fs_ncg * super->fs_ipg - 1;
427         for (inode = 0; inode < maxino; inode++) {
428                 errno = 0;
429                 if ((ip = get_inode(fd,super,inode))
430                     && !isfree(ip))
431                         uses(ip->di_uid,
432                             estimate ? virtualblocks(super,ip) :
433                                 actualblocks(super,ip),
434                             ip->di_atime);
435                 else if (errno) {
436                         err(1, "%s", name);
437                 }
438         }
439         if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
440                 errx(1, "allocate users");
441         bcopy(users,usrs,nusers * sizeof(struct user));
442         sortusers(usrs);
443         for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
444                 printf("%5d",SIZE(usr->space));
445                 if (count)
446                         printf("\t%5ld",usr->count);
447                 printf("\t%-8s",usr->name);
448                 if (unused)
449                         printf("\t%5d\t%5d\t%5d",
450                                SIZE(usr->spc30),
451                                SIZE(usr->spc60),
452                                SIZE(usr->spc90));
453                 printf("\n");
454         }
455         free(usrs);
456 }
457
458 static void
459 donames(fd,super,name)
460         int fd;
461         struct fs *super;
462         char *name;
463 {
464         int c;
465         ino_t inode, inode1;
466         ino_t maxino;
467         struct dinode *ip;
468         
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');
473         ungetc(c,stdin);
474         inode1 = -1;
475         while (scanf("%u",&inode) == 1) {
476                 if (inode > maxino) {
477                         warnx("illegal inode %d",inode);
478                         return;
479                 }
480                 errno = 0;
481                 if ((ip = get_inode(fd,super,inode))
482                     && !isfree(ip)) {
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') {
488                                 putchar(c);
489                                 c = getchar();
490                         }
491                         putchar('\n');
492                         inode1 = inode;
493                 } else {
494                         if (errno) {
495                                 err(1, "%s", name);
496                         }
497                         /* skip this line */
498                         while ((c = getchar()) != EOF && c != '\n');
499                 }
500                 if (c == EOF)
501                         break;
502         }
503 }
504
505 static void
506 usage()
507 {
508 #ifdef  COMPAT
509         fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n");
510 #else   /* COMPAT */
511         fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n");
512 #endif  /* COMPAT */
513         exit(1);
514 }
515
516 static char superblock[SBSIZE];
517
518 void
519 quot(name,mp)
520         char *name, *mp;
521 {
522         int fd;
523         
524         get_inode(-1, NULL, 0);         /* flush cache */
525         inituser();
526         initfsizes();
527         if ((fd = open(name,0)) < 0
528             || lseek(fd,SBOFF,0) != SBOFF
529             || read(fd,superblock,SBSIZE) != SBSIZE) {
530                 warn("%s", name);
531                 close(fd);
532                 return;
533         }
534         if (((struct fs *)superblock)->fs_magic != FS_MAGIC) {
535                 warnx("%s: not a BSD filesystem",name);
536                 close(fd);
537                 return;
538         }
539         printf("%s:",name);
540         if (mp)
541                 printf(" (%s)",mp);
542         putchar('\n');
543         (*func)(fd,(struct fs *)superblock,name);
544         close(fd);
545 }
546
547 int
548 main(argc,argv)
549         int argc;
550         char **argv;
551 {
552         char all = 0;
553         struct statfs *mp;
554         struct fstab *fs;
555         char dev[MNAMELEN + 1];
556         char *nm;
557         int cnt;
558         
559         func = douser;
560 #ifndef COMPAT
561         header = getbsize(&headerlen,&blocksize);
562 #endif
563         while (--argc > 0 && **++argv == '-') {
564                 while (*++*argv) {
565                         switch (**argv) {
566                         case 'n':
567                                 func = donames;
568                                 break;
569                         case 'c':
570                                 func = dofsizes;
571                                 break;
572                         case 'a':
573                                 all = 1;
574                                 break;
575                         case 'f':
576                                 count = 1;
577                                 break;
578                         case 'h':
579                                 estimate = 1;
580                                 break;
581 #ifndef COMPAT
582                         case 'k':
583                                 blocksize = 1024;
584                                 break;
585 #endif  /* COMPAT */
586                         case 'v':
587                                 unused = 1;
588                                 break;
589                         default:
590                                 usage();
591                         }
592                 }
593         }
594         if (all) {
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);
600                                         nm = dev;
601                                 } else
602                                         nm = mp->f_mntfromname;
603                                 quot(nm,mp->f_mntonname);
604                         }
605                 }
606         }
607         while (--argc >= 0) {
608                 if ((fs = getfsfile(*argv)) != NULL)
609                         quot(fs->fs_spec, 0);
610                 else
611                         quot(*argv,0);
612                 argv++;
613         }
614         return 0;
615 }