quot.c (96638) | quot.c (98542) |
---|---|
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: --- 17 unchanged lines hidden (view full) --- 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 33static const char rcsid[] = | 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: --- 17 unchanged lines hidden (view full) --- 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 33static const char rcsid[] = |
34 "$FreeBSD: head/usr.sbin/quot/quot.c 96638 2002-05-15 08:01:31Z des $"; | 34 "$FreeBSD: head/usr.sbin/quot/quot.c 98542 2002-06-21 06:18:05Z mckusick $"; |
35#endif /* not lint */ 36 37#include <sys/param.h> 38#include <sys/stdint.h> 39#include <sys/mount.h> 40#include <sys/disklabel.h> 41#include <sys/time.h> | 35#endif /* not lint */ 36 37#include <sys/param.h> 38#include <sys/stdint.h> 39#include <sys/mount.h> 40#include <sys/disklabel.h> 41#include <sys/time.h> |
42#include <ufs/ufs/dinode.h> |
|
42#include <ufs/ffs/fs.h> | 43#include <ufs/ffs/fs.h> |
43#include <ufs/ufs/quota.h> 44#include <ufs/ufs/inode.h> | |
45 46#include <err.h> 47#include <fcntl.h> 48#include <fstab.h> 49#include <errno.h> 50#include <paths.h> 51#include <pwd.h> 52#include <stdio.h> --- 5 unchanged lines hidden (view full) --- 58static char estimate; 59static char count; 60static char unused; 61static void (*func)(int, struct fs *, char *); 62static long blocksize; 63static char *header; 64static int headerlen; 65 | 44 45#include <err.h> 46#include <fcntl.h> 47#include <fstab.h> 48#include <errno.h> 49#include <paths.h> 50#include <pwd.h> 51#include <stdio.h> --- 5 unchanged lines hidden (view full) --- 57static char estimate; 58static char count; 59static char unused; 60static void (*func)(int, struct fs *, char *); 61static long blocksize; 62static char *header; 63static int headerlen; 64 |
66static struct dinode *get_inode(int, struct fs *, ino_t); 67static int virtualblocks(struct fs *, struct dinode *); 68static int isfree(struct dinode *); | 65static union dinode *get_inode(int, struct fs *, ino_t); 66static int virtualblocks(struct fs *, union dinode *); 67static int isfree(struct fs *, union dinode *); |
69static void inituser(void); 70static void usrrehash(void); 71static struct user *user(uid_t); 72static int cmpusers(const void *, const void *); 73static void uses(uid_t, daddr_t, time_t); 74static void initfsizes(void); 75static void dofsizes(int, struct fs *, char *); 76static void douser(int, struct fs *, char *); --- 14 unchanged lines hidden (view full) --- 91 */ 92#ifdef COMPAT 93#define SIZE(n) (n) 94#else 95#define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize)) 96#endif 97 98#define INOCNT(fs) ((fs)->fs_ipg) | 68static void inituser(void); 69static void usrrehash(void); 70static struct user *user(uid_t); 71static int cmpusers(const void *, const void *); 72static void uses(uid_t, daddr_t, time_t); 73static void initfsizes(void); 74static void dofsizes(int, struct fs *, char *); 75static void douser(int, struct fs *, char *); --- 14 unchanged lines hidden (view full) --- 90 */ 91#ifdef COMPAT 92#define SIZE(n) (n) 93#else 94#define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize)) 95#endif 96 97#define INOCNT(fs) ((fs)->fs_ipg) |
99#define INOSZ(fs) (sizeof(struct dinode) * INOCNT(fs)) | 98#define INOSZ(fs) \ 99 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \ 100 sizeof(struct ufs2_dinode)) * INOCNT(fs)) |
100 | 101 |
101static struct dinode * | 102union dinode { 103 struct ufs1_dinode dp1; 104 struct ufs2_dinode dp2; 105}; 106#define DIP(fs, dp, field) \ 107 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \ 108 (dp)->dp1.field : (dp)->dp2.field) 109 110static union dinode * |
102get_inode(fd,super,ino) 103 int fd; 104 struct fs *super; 105 ino_t ino; 106{ | 111get_inode(fd,super,ino) 112 int fd; 113 struct fs *super; 114 ino_t ino; 115{ |
107 static struct dinode *ip; | 116 static caddr_t ipbuf; |
108 static ino_t last; 109 110 if (fd < 0) { /* flush cache */ | 117 static ino_t last; 118 119 if (fd < 0) { /* flush cache */ |
111 if (ip) { 112 free(ip); 113 ip = 0; | 120 if (ipbuf) { 121 free(ipbuf); 122 ipbuf = 0; |
114 } 115 return 0; 116 } 117 | 123 } 124 return 0; 125 } 126 |
118 if (!ip || ino < last || ino >= last + INOCNT(super)) { 119 if (!ip 120 && !(ip = (struct dinode *)malloc(INOSZ(super)))) | 127 if (!ipbuf || ino < last || ino >= last + INOCNT(super)) { 128 if (!ipbuf 129 && !(ipbuf = malloc(INOSZ(super)))) |
121 errx(1, "allocate inodes"); 122 last = (ino / INOCNT(super)) * INOCNT(super); 123 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0 | 130 errx(1, "allocate inodes"); 131 last = (ino / INOCNT(super)) * INOCNT(super); 132 if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0 |
124 || read(fd,ip,INOSZ(super)) != (ssize_t)INOSZ(super)) | 133 || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super)) |
125 err(1, "read inodes"); 126 } 127 | 134 err(1, "read inodes"); 135 } 136 |
128 return ip + ino % INOCNT(super); | 137 if (super->fs_magic == FS_UFS1_MAGIC) 138 return ((union dinode *) 139 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]); 140 return ((union dinode *) 141 &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]); |
129} 130 131#ifdef COMPAT | 142} 143 144#ifdef COMPAT |
132#define actualblocks(super,ip) ((ip)->di_blocks/2) | 145#define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2) |
133#else | 146#else |
134#define actualblocks(super,ip) ((ip)->di_blocks) | 147#define actualblocks(fs, dp) DIP(fs, dp, di_blocks) |
135#endif 136 | 148#endif 149 |
137static int virtualblocks(super,ip) | 150static int virtualblocks(super, dp) |
138 struct fs *super; | 151 struct fs *super; |
139 struct dinode *ip; | 152 union dinode *dp; |
140{ 141 register off_t nblk, sz; 142 | 153{ 154 register off_t nblk, sz; 155 |
143 sz = ip->di_size; | 156 sz = DIP(super, dp, di_size); |
144#ifdef COMPAT 145 if (lblkno(super,sz) >= NDADDR) { 146 nblk = blkroundup(super,sz); 147 if (sz == nblk) 148 nblk += super->fs_bsize; 149 } 150 151 return sz / 1024; --- 12 unchanged lines hidden (view full) --- 164 } else 165 nblk = fragroundup(super,sz); 166 167 return nblk / 512; 168#endif /* COMPAT */ 169} 170 171static int | 157#ifdef COMPAT 158 if (lblkno(super,sz) >= NDADDR) { 159 nblk = blkroundup(super,sz); 160 if (sz == nblk) 161 nblk += super->fs_bsize; 162 } 163 164 return sz / 1024; --- 12 unchanged lines hidden (view full) --- 177 } else 178 nblk = fragroundup(super,sz); 179 180 return nblk / 512; 181#endif /* COMPAT */ 182} 183 184static int |
172isfree(ip) 173 struct dinode *ip; | 185isfree(super, dp) 186 struct fs *super; 187 union dinode *dp; |
174{ 175#ifdef COMPAT | 188{ 189#ifdef COMPAT |
176 return (ip->di_mode&IFMT) == 0; | 190 return (DIP(super, dp, di_mode) & IFMT) == 0; |
177#else /* COMPAT */ 178 | 191#else /* COMPAT */ 192 |
179 switch (ip->di_mode&IFMT) { | 193 switch (DIP(super, dp, di_mode) & IFMT) { |
180 case IFIFO: 181 case IFLNK: /* should check FASTSYMLINK? */ 182 case IFDIR: 183 case IFREG: 184 return 0; 185 default: 186 return 1; 187 } --- 148 unchanged lines hidden (view full) --- 336 for (i = FSZCNT; --i >= 0;) { 337 fp->fsz_count[i] = 0; 338 fp->fsz_sz[i] = 0; 339 } 340 } 341} 342 343static void | 194 case IFIFO: 195 case IFLNK: /* should check FASTSYMLINK? */ 196 case IFDIR: 197 case IFREG: 198 return 0; 199 default: 200 return 1; 201 } --- 148 unchanged lines hidden (view full) --- 350 for (i = FSZCNT; --i >= 0;) { 351 fp->fsz_count[i] = 0; 352 fp->fsz_sz[i] = 0; 353 } 354 } 355} 356 357static void |
344dofsizes(fd,super,name) | 358dofsizes(fd, super, name) |
345 int fd; 346 struct fs *super; 347 char *name; 348{ 349 ino_t inode, maxino; | 359 int fd; 360 struct fs *super; 361 char *name; 362{ 363 ino_t inode, maxino; |
350 struct dinode *ip; | 364 union dinode *dp; |
351 daddr_t sz, ksz; 352 struct fsizes *fp, **fsp; 353 register int i; 354 355 maxino = super->fs_ncg * super->fs_ipg - 1; 356#ifdef COMPAT 357 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes)))) 358 errx(1, "allocate fsize structure"); 359#endif /* COMPAT */ 360 for (inode = 0; inode < maxino; inode++) { 361 errno = 0; | 365 daddr_t sz, ksz; 366 struct fsizes *fp, **fsp; 367 register int i; 368 369 maxino = super->fs_ncg * super->fs_ipg - 1; 370#ifdef COMPAT 371 if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes)))) 372 errx(1, "allocate fsize structure"); 373#endif /* COMPAT */ 374 for (inode = 0; inode < maxino; inode++) { 375 errno = 0; |
362 if ((ip = get_inode(fd,super,inode)) | 376 if ((dp = get_inode(fd,super,inode)) |
363#ifdef COMPAT | 377#ifdef COMPAT |
364 && ((ip->di_mode&IFMT) == IFREG 365 || (ip->di_mode&IFMT) == IFDIR) | 378 && ((DIP(super, dp, di_mode) & IFMT) == IFREG 379 || (DIP(super, dp, di_mode) & IFMT) == IFDIR) |
366#else /* COMPAT */ | 380#else /* COMPAT */ |
367 && !isfree(ip) | 381 && !isfree(super, dp) |
368#endif /* COMPAT */ 369 ) { | 382#endif /* COMPAT */ 383 ) { |
370 sz = estimate ? virtualblocks(super,ip) : 371 actualblocks(super,ip); | 384 sz = estimate ? virtualblocks(super, dp) : 385 actualblocks(super, dp); |
372#ifdef COMPAT 373 if (sz >= FSZCNT) { 374 fsizes->fsz_count[FSZCNT-1]++; 375 fsizes->fsz_sz[FSZCNT-1] += sz; 376 } else { 377 fsizes->fsz_count[sz]++; 378 fsizes->fsz_sz[sz] += sz; 379 } --- 31 unchanged lines hidden (view full) --- 411 (intmax_t)(fp->fsz_first + i), 412 (intmax_t)fp->fsz_count[i], 413 SIZE(sz += fp->fsz_sz[i])); 414 } 415 } 416} 417 418static void | 386#ifdef COMPAT 387 if (sz >= FSZCNT) { 388 fsizes->fsz_count[FSZCNT-1]++; 389 fsizes->fsz_sz[FSZCNT-1] += sz; 390 } else { 391 fsizes->fsz_count[sz]++; 392 fsizes->fsz_sz[sz] += sz; 393 } --- 31 unchanged lines hidden (view full) --- 425 (intmax_t)(fp->fsz_first + i), 426 (intmax_t)fp->fsz_count[i], 427 SIZE(sz += fp->fsz_sz[i])); 428 } 429 } 430} 431 432static void |
419douser(fd,super,name) | 433douser(fd, super, name) |
420 int fd; 421 struct fs *super; 422 char *name; 423{ 424 ino_t inode, maxino; 425 struct user *usr, *usrs; | 434 int fd; 435 struct fs *super; 436 char *name; 437{ 438 ino_t inode, maxino; 439 struct user *usr, *usrs; |
426 struct dinode *ip; | 440 union dinode *dp; |
427 register int n; 428 429 maxino = super->fs_ncg * super->fs_ipg - 1; 430 for (inode = 0; inode < maxino; inode++) { 431 errno = 0; | 441 register int n; 442 443 maxino = super->fs_ncg * super->fs_ipg - 1; 444 for (inode = 0; inode < maxino; inode++) { 445 errno = 0; |
432 if ((ip = get_inode(fd,super,inode)) 433 && !isfree(ip)) 434 uses(ip->di_uid, 435 estimate ? virtualblocks(super,ip) : 436 actualblocks(super,ip), 437 ip->di_atime); | 446 if ((dp = get_inode(fd,super,inode)) 447 && !isfree(super, dp)) 448 uses(DIP(super, dp, di_uid), 449 estimate ? virtualblocks(super, dp) : 450 actualblocks(super, dp), 451 DIP(super, dp, di_atime)); |
438 else if (errno) { 439 err(1, "%s", name); 440 } 441 } 442 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user)))) 443 errx(1, "allocate users"); 444 bcopy(users,usrs,nusers * sizeof(struct user)); 445 sortusers(usrs); --- 8 unchanged lines hidden (view full) --- 454 SIZE(usr->spc60), 455 SIZE(usr->spc90)); 456 printf("\n"); 457 } 458 free(usrs); 459} 460 461static void | 452 else if (errno) { 453 err(1, "%s", name); 454 } 455 } 456 if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user)))) 457 errx(1, "allocate users"); 458 bcopy(users,usrs,nusers * sizeof(struct user)); 459 sortusers(usrs); --- 8 unchanged lines hidden (view full) --- 468 SIZE(usr->spc60), 469 SIZE(usr->spc90)); 470 printf("\n"); 471 } 472 free(usrs); 473} 474 475static void |
462donames(fd,super,name) | 476donames(fd, super, name) |
463 int fd; 464 struct fs *super; 465 char *name; 466{ 467 int c; 468 ino_t inode, inode1; 469 ino_t maxino; | 477 int fd; 478 struct fs *super; 479 char *name; 480{ 481 int c; 482 ino_t inode, inode1; 483 ino_t maxino; |
470 struct dinode *ip; | 484 union dinode *dp; |
471 472 maxino = super->fs_ncg * super->fs_ipg - 1; 473 /* first skip the name of the filesystem */ 474 while ((c = getchar()) != EOF && (c < '0' || c > '9')) 475 while ((c = getchar()) != EOF && c != '\n'); 476 ungetc(c,stdin); 477 inode1 = -1; 478 while (scanf("%u",&inode) == 1) { 479 if (inode > maxino) { 480 warnx("illegal inode %d",inode); 481 return; 482 } 483 errno = 0; | 485 486 maxino = super->fs_ncg * super->fs_ipg - 1; 487 /* first skip the name of the filesystem */ 488 while ((c = getchar()) != EOF && (c < '0' || c > '9')) 489 while ((c = getchar()) != EOF && c != '\n'); 490 ungetc(c,stdin); 491 inode1 = -1; 492 while (scanf("%u",&inode) == 1) { 493 if (inode > maxino) { 494 warnx("illegal inode %d",inode); 495 return; 496 } 497 errno = 0; |
484 if ((ip = get_inode(fd,super,inode)) 485 && !isfree(ip)) { 486 printf("%s\t",user(ip->di_uid)->name); | 498 if ((dp = get_inode(fd,super,inode)) 499 && !isfree(super, dp)) { 500 printf("%s\t",user(DIP(super, dp, di_uid))->name); |
487 /* now skip whitespace */ 488 while ((c = getchar()) == ' ' || c == '\t'); 489 /* and print out the remainder of the input line */ 490 while (c != EOF && c != '\n') { 491 putchar(c); 492 c = getchar(); 493 } 494 putchar('\n'); --- 16 unchanged lines hidden (view full) --- 511#ifdef COMPAT 512 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n"); 513#else /* COMPAT */ 514 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n"); 515#endif /* COMPAT */ 516 exit(1); 517} 518 | 501 /* now skip whitespace */ 502 while ((c = getchar()) == ' ' || c == '\t'); 503 /* and print out the remainder of the input line */ 504 while (c != EOF && c != '\n') { 505 putchar(c); 506 c = getchar(); 507 } 508 putchar('\n'); --- 16 unchanged lines hidden (view full) --- 525#ifdef COMPAT 526 fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n"); 527#else /* COMPAT */ 528 fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n"); 529#endif /* COMPAT */ 530 exit(1); 531} 532 |
519static char superblock[SBSIZE]; | 533/* 534 * Possible superblock locations ordered from most to least likely. 535 */ 536static int sblock_try[] = SBLOCKSEARCH; 537static char superblock[SBLOCKSIZE]; |
520 521void 522quot(name,mp) 523 char *name, *mp; 524{ | 538 539void 540quot(name,mp) 541 char *name, *mp; 542{ |
525 int fd; | 543 int i, fd; 544 struct fs *fs; |
526 527 get_inode(-1, NULL, 0); /* flush cache */ 528 inituser(); 529 initfsizes(); | 545 546 get_inode(-1, NULL, 0); /* flush cache */ 547 inituser(); 548 initfsizes(); |
530 if ((fd = open(name,0)) < 0 531 || lseek(fd,SBOFF,0) != SBOFF 532 || read(fd,superblock,SBSIZE) != SBSIZE) { | 549 if ((fd = open(name,0)) < 0) { |
533 warn("%s", name); 534 close(fd); 535 return; 536 } | 550 warn("%s", name); 551 close(fd); 552 return; 553 } |
537 if (((struct fs *)superblock)->fs_magic != FS_MAGIC) { | 554 for (i = 0; sblock_try[i] != -1; i++) { 555 if (lseek(fd, sblock_try[i], 0) != sblock_try[i]) { 556 close(fd); 557 return; 558 } 559 if (read(fd, superblock, SBLOCKSIZE) != SBLOCKSIZE) { 560 close(fd); 561 return; 562 } 563 fs = (struct fs *)superblock; 564 if ((fs->fs_magic == FS_UFS1_MAGIC || 565 (fs->fs_magic == FS_UFS2_MAGIC && 566 fs->fs_sblockloc == numfrags(fs, sblock_try[i]))) && 567 fs->fs_bsize <= MAXBSIZE && 568 fs->fs_bsize >= sizeof(struct fs)) 569 break; 570 } 571 if (sblock_try[i] == -1) { |
538 warnx("%s: not a BSD filesystem",name); 539 close(fd); 540 return; 541 } 542 printf("%s:",name); 543 if (mp) 544 printf(" (%s)",mp); 545 putchar('\n'); | 572 warnx("%s: not a BSD filesystem",name); 573 close(fd); 574 return; 575 } 576 printf("%s:",name); 577 if (mp) 578 printf(" (%s)",mp); 579 putchar('\n'); |
546 (*func)(fd,(struct fs *)superblock,name); | 580 (*func)(fd, fs, name); |
547 close(fd); 548} 549 550int 551main(argc,argv) 552 int argc; 553 char **argv; 554{ --- 64 unchanged lines hidden --- | 581 close(fd); 582} 583 584int 585main(argc,argv) 586 int argc; 587 char **argv; 588{ --- 64 unchanged lines hidden --- |