quot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*-
  2. * SPDX-License-Identifier: BSD-4-Clause
  3. *
  4. * Copyright (C) 1991, 1994 Wolfgang Solfrank.
  5. * Copyright (C) 1991, 1994 TooLs GmbH.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by TooLs GmbH.
  19. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/param.h>
  34. #include <sys/stdint.h>
  35. #include <sys/mount.h>
  36. #include <sys/disklabel.h>
  37. #include <ufs/ufs/dinode.h>
  38. #include <ufs/ffs/fs.h>
  39. #include <err.h>
  40. #include <fcntl.h>
  41. #include <fstab.h>
  42. #include <errno.h>
  43. #include <libufs.h>
  44. #include <paths.h>
  45. #include <pwd.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <time.h>
  50. #include <unistd.h>
  51. /* some flags of what to do: */
  52. static char estimate;
  53. static char count;
  54. static char unused;
  55. static void (*func)(int, struct fs *, char *);
  56. static long blocksize;
  57. static char *header;
  58. static int headerlen;
  59. static union dinode *get_inode(int, struct fs *, ino_t);
  60. static int virtualblocks(struct fs *, union dinode *);
  61. static int isfree(struct fs *, union dinode *);
  62. static void inituser(void);
  63. static void usrrehash(void);
  64. static struct user *user(uid_t);
  65. static int cmpusers(const void *, const void *);
  66. static void uses(uid_t, daddr_t, time_t);
  67. static void initfsizes(void);
  68. static void dofsizes(int, struct fs *, char *);
  69. static void douser(int, struct fs *, char *);
  70. static void donames(int, struct fs *, char *);
  71. static void usage(void);
  72. static void quot(char *, char *);
  73. /*
  74. * Original BSD quot doesn't round to number of frags/blocks,
  75. * doesn't account for indirection blocks and gets it totally
  76. * wrong if the size is a multiple of the blocksize.
  77. * The new code always counts the number of 512 byte blocks
  78. * instead of the number of kilobytes and converts them to
  79. * kByte when done (on request).
  80. *
  81. * Due to the size of modern disks, we must cast intermediate
  82. * values to 64 bits to prevent potential overflows.
  83. */
  84. #ifdef COMPAT
  85. #define SIZE(n) (n)
  86. #else
  87. #define SIZE(n) ((int)(((quad_t)(n) * 512 + blocksize - 1)/blocksize))
  88. #endif
  89. #define INOCNT(fs) ((fs)->fs_ipg)
  90. #define INOSZ(fs) \
  91. (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
  92. sizeof(struct ufs2_dinode)) * INOCNT(fs))
  93. union dinode {
  94. struct ufs1_dinode dp1;
  95. struct ufs2_dinode dp2;
  96. };
  97. #define DIP(fs, dp, field) \
  98. (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
  99. (dp)->dp1.field : (dp)->dp2.field)
  100. static union dinode *
  101. get_inode(int fd, struct fs *super, ino_t ino)
  102. {
  103. static caddr_t ipbuf;
  104. static struct cg *cgp;
  105. static ino_t last;
  106. static int cg;
  107. struct ufs2_dinode *di2;
  108. if (fd < 0) { /* flush cache */
  109. if (ipbuf) {
  110. free(ipbuf);
  111. ipbuf = 0;
  112. if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) {
  113. free(cgp);
  114. cgp = 0;
  115. }
  116. }
  117. return 0;
  118. }
  119. if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
  120. if (super->fs_magic == FS_UFS2_MAGIC &&
  121. (!cgp || cg != ino_to_cg(super, ino))) {
  122. cg = ino_to_cg(super, ino);
  123. if (!cgp && !(cgp = malloc(super->fs_cgsize)))
  124. errx(1, "allocate cg");
  125. if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
  126. err(1, "lseek cg");
  127. if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
  128. err(1, "read cg");
  129. if (!cg_chkmagic(cgp))
  130. errx(1, "cg has bad magic");
  131. }
  132. if (!ipbuf
  133. && !(ipbuf = malloc(INOSZ(super))))
  134. errx(1, "allocate inodes");
  135. last = rounddown(ino, INOCNT(super));
  136. if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0
  137. || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
  138. err(1, "read inodes");
  139. }
  140. if (super->fs_magic == FS_UFS1_MAGIC)
  141. return ((union dinode *)
  142. &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
  143. di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
  144. /* If the inode is unused, it might be unallocated too, so zero it. */
  145. if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
  146. bzero(di2, sizeof (*di2));
  147. return ((union dinode *)di2);
  148. }
  149. #ifdef COMPAT
  150. #define actualblocks(fs, dp) (DIP(fs, dp, di_blocks) / 2)
  151. #else
  152. #define actualblocks(fs, dp) DIP(fs, dp, di_blocks)
  153. #endif
  154. static int virtualblocks(struct fs *super, union dinode *dp)
  155. {
  156. off_t nblk, sz;
  157. sz = DIP(super, dp, di_size);
  158. #ifdef COMPAT
  159. if (lblkno(super,sz) >= UFS_NDADDR) {
  160. nblk = blkroundup(super,sz);
  161. if (sz == nblk)
  162. nblk += super->fs_bsize;
  163. }
  164. return sz / 1024;
  165. #else /* COMPAT */
  166. if (lblkno(super,sz) >= UFS_NDADDR) {
  167. nblk = blkroundup(super,sz);
  168. sz = lblkno(super,nblk);
  169. sz = (sz - UFS_NDADDR + NINDIR(super) - 1) / NINDIR(super);
  170. while (sz > 0) {
  171. nblk += sz * super->fs_bsize;
  172. /* sz - 1 rounded up */
  173. sz = (sz - 1 + NINDIR(super) - 1) / NINDIR(super);
  174. }
  175. } else
  176. nblk = fragroundup(super,sz);
  177. return nblk / 512;
  178. #endif /* COMPAT */
  179. }
  180. static int
  181. isfree(struct fs *super, union dinode *dp)
  182. {
  183. #ifdef COMPAT
  184. return (DIP(super, dp, di_mode) & IFMT) == 0;
  185. #else /* COMPAT */
  186. switch (DIP(super, dp, di_mode) & IFMT) {
  187. case IFIFO:
  188. case IFLNK: /* should check FASTSYMLINK? */
  189. case IFDIR:
  190. case IFREG:
  191. return 0;
  192. case IFCHR:
  193. case IFBLK:
  194. case IFSOCK:
  195. case IFWHT:
  196. case 0:
  197. return 1;
  198. default:
  199. errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
  200. }
  201. #endif
  202. }
  203. static struct user {
  204. uid_t uid;
  205. char *name;
  206. daddr_t space;
  207. long count;
  208. daddr_t spc30;
  209. daddr_t spc60;
  210. daddr_t spc90;
  211. } *users;
  212. static int nusers;
  213. static void
  214. inituser(void)
  215. {
  216. int i;
  217. struct user *usr;
  218. if (!nusers) {
  219. nusers = 8;
  220. if (!(users =
  221. (struct user *)calloc(nusers,sizeof(struct user))))
  222. errx(1, "allocate users");
  223. } else {
  224. for (usr = users, i = nusers; --i >= 0; usr++) {
  225. usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
  226. usr->count = 0;
  227. }
  228. }
  229. }
  230. static void
  231. usrrehash(void)
  232. {
  233. int i;
  234. struct user *usr, *usrn;
  235. struct user *svusr;
  236. svusr = users;
  237. nusers <<= 1;
  238. if (!(users = (struct user *)calloc(nusers,sizeof(struct user))))
  239. errx(1, "allocate users");
  240. for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
  241. for (usrn = users + (usr->uid&(nusers - 1)); usrn->name;
  242. usrn--) {
  243. if (usrn <= users)
  244. usrn = users + nusers;
  245. }
  246. *usrn = *usr;
  247. }
  248. }
  249. static struct user *
  250. user(uid_t uid)
  251. {
  252. struct user *usr;
  253. int i;
  254. struct passwd *pwd;
  255. while (1) {
  256. for (usr = users + (uid&(nusers - 1)), i = nusers; --i >= 0;
  257. usr--) {
  258. if (!usr->name) {
  259. usr->uid = uid;
  260. if (!(pwd = getpwuid(uid))) {
  261. if ((usr->name = (char *)malloc(7)))
  262. sprintf(usr->name,"#%d",uid);
  263. } else {
  264. if ((usr->name = (char *)
  265. malloc(strlen(pwd->pw_name) + 1)))
  266. strcpy(usr->name,pwd->pw_name);
  267. }
  268. if (!usr->name)
  269. errx(1, "allocate users");
  270. return usr;
  271. } else if (usr->uid == uid)
  272. return usr;
  273. if (usr <= users)
  274. usr = users + nusers;
  275. }
  276. usrrehash();
  277. }
  278. }
  279. static int
  280. cmpusers(const void *v1, const void *v2)
  281. {
  282. const struct user *u1, *u2;
  283. u1 = (const struct user *)v1;
  284. u2 = (const struct user *)v2;
  285. return u2->space - u1->space;
  286. }
  287. #define sortusers(users) (qsort((users),nusers,sizeof(struct user), \
  288. cmpusers))
  289. static void
  290. uses(uid_t uid, daddr_t blks, time_t act)
  291. {
  292. static time_t today;
  293. struct user *usr;
  294. if (!today)
  295. time(&today);
  296. usr = user(uid);
  297. usr->count++;
  298. usr->space += blks;
  299. if (today - act > 90L * 24L * 60L * 60L)
  300. usr->spc90 += blks;
  301. if (today - act > 60L * 24L * 60L * 60L)
  302. usr->spc60 += blks;
  303. if (today - act > 30L * 24L * 60L * 60L)
  304. usr->spc30 += blks;
  305. }
  306. #ifdef COMPAT
  307. #define FSZCNT 500
  308. #else
  309. #define FSZCNT 512
  310. #endif
  311. struct fsizes {
  312. struct fsizes *fsz_next;
  313. daddr_t fsz_first, fsz_last;
  314. ino_t fsz_count[FSZCNT];
  315. daddr_t fsz_sz[FSZCNT];
  316. } *fsizes;
  317. static void
  318. initfsizes(void)
  319. {
  320. struct fsizes *fp;
  321. int i;
  322. for (fp = fsizes; fp; fp = fp->fsz_next) {
  323. for (i = FSZCNT; --i >= 0;) {
  324. fp->fsz_count[i] = 0;
  325. fp->fsz_sz[i] = 0;
  326. }
  327. }
  328. }
  329. static void
  330. dofsizes(int fd, struct fs *super, char *name)
  331. {
  332. ino_t inode, maxino;
  333. union dinode *dp;
  334. daddr_t sz, ksz;
  335. struct fsizes *fp, **fsp;
  336. int i;
  337. maxino = super->fs_ncg * super->fs_ipg - 1;
  338. #ifdef COMPAT
  339. if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes))))
  340. errx(1, "allocate fsize structure");
  341. #endif /* COMPAT */
  342. for (inode = 0; inode < maxino; inode++) {
  343. errno = 0;
  344. if ((dp = get_inode(fd,super,inode))
  345. #ifdef COMPAT
  346. && ((DIP(super, dp, di_mode) & IFMT) == IFREG
  347. || (DIP(super, dp, di_mode) & IFMT) == IFDIR)
  348. #else /* COMPAT */
  349. && !isfree(super, dp)
  350. #endif /* COMPAT */
  351. ) {
  352. sz = estimate ? virtualblocks(super, dp) :
  353. actualblocks(super, dp);
  354. #ifdef COMPAT
  355. if (sz >= FSZCNT) {
  356. fsizes->fsz_count[FSZCNT-1]++;
  357. fsizes->fsz_sz[FSZCNT-1] += sz;
  358. } else {
  359. fsizes->fsz_count[sz]++;
  360. fsizes->fsz_sz[sz] += sz;
  361. }
  362. #else /* COMPAT */
  363. ksz = SIZE(sz);
  364. for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
  365. if (ksz < fp->fsz_last)
  366. break;
  367. }
  368. if (!fp || ksz < fp->fsz_first) {
  369. if (!(fp = (struct fsizes *)
  370. malloc(sizeof(struct fsizes))))
  371. errx(1, "allocate fsize structure");
  372. fp->fsz_next = *fsp;
  373. *fsp = fp;
  374. fp->fsz_first = rounddown(ksz, FSZCNT);
  375. fp->fsz_last = fp->fsz_first + FSZCNT;
  376. for (i = FSZCNT; --i >= 0;) {
  377. fp->fsz_count[i] = 0;
  378. fp->fsz_sz[i] = 0;
  379. }
  380. }
  381. fp->fsz_count[ksz % FSZCNT]++;
  382. fp->fsz_sz[ksz % FSZCNT] += sz;
  383. #endif /* COMPAT */
  384. } else if (errno) {
  385. err(1, "%s", name);
  386. }
  387. }
  388. sz = 0;
  389. for (fp = fsizes; fp; fp = fp->fsz_next) {
  390. for (i = 0; i < FSZCNT; i++) {
  391. if (fp->fsz_count[i])
  392. printf("%jd\t%jd\t%d\n",
  393. (intmax_t)(fp->fsz_first + i),
  394. (intmax_t)fp->fsz_count[i],
  395. SIZE(sz += fp->fsz_sz[i]));
  396. }
  397. }
  398. }
  399. static void
  400. douser(int fd, struct fs *super, char *name)
  401. {
  402. ino_t inode, maxino;
  403. struct user *usr, *usrs;
  404. union dinode *dp;
  405. int n;
  406. maxino = super->fs_ncg * super->fs_ipg - 1;
  407. for (inode = 0; inode < maxino; inode++) {
  408. errno = 0;
  409. if ((dp = get_inode(fd,super,inode))
  410. && !isfree(super, dp))
  411. uses(DIP(super, dp, di_uid),
  412. estimate ? virtualblocks(super, dp) :
  413. actualblocks(super, dp),
  414. DIP(super, dp, di_atime));
  415. else if (errno) {
  416. err(1, "%s", name);
  417. }
  418. }
  419. if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user))))
  420. errx(1, "allocate users");
  421. bcopy(users,usrs,nusers * sizeof(struct user));
  422. sortusers(usrs);
  423. for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
  424. printf("%5d",SIZE(usr->space));
  425. if (count)
  426. printf("\t%5ld",usr->count);
  427. printf("\t%-8s",usr->name);
  428. if (unused)
  429. printf("\t%5d\t%5d\t%5d",
  430. SIZE(usr->spc30),
  431. SIZE(usr->spc60),
  432. SIZE(usr->spc90));
  433. printf("\n");
  434. }
  435. free(usrs);
  436. }
  437. static void
  438. donames(int fd, struct fs *super, char *name)
  439. {
  440. int c;
  441. ino_t maxino;
  442. uintmax_t inode;
  443. union dinode *dp;
  444. maxino = super->fs_ncg * super->fs_ipg - 1;
  445. /* first skip the name of the filesystem */
  446. while ((c = getchar()) != EOF && (c < '0' || c > '9'))
  447. while ((c = getchar()) != EOF && c != '\n');
  448. ungetc(c,stdin);
  449. while (scanf("%ju", &inode) == 1) {
  450. if (inode > maxino) {
  451. warnx("illegal inode %ju", inode);
  452. return;
  453. }
  454. errno = 0;
  455. if ((dp = get_inode(fd,super,inode))
  456. && !isfree(super, dp)) {
  457. printf("%s\t",user(DIP(super, dp, di_uid))->name);
  458. /* now skip whitespace */
  459. while ((c = getchar()) == ' ' || c == '\t');
  460. /* and print out the remainder of the input line */
  461. while (c != EOF && c != '\n') {
  462. putchar(c);
  463. c = getchar();
  464. }
  465. putchar('\n');
  466. } else {
  467. if (errno) {
  468. err(1, "%s", name);
  469. }
  470. /* skip this line */
  471. while ((c = getchar()) != EOF && c != '\n');
  472. }
  473. if (c == EOF)
  474. break;
  475. }
  476. }
  477. static void
  478. usage(void)
  479. {
  480. #ifdef COMPAT
  481. fprintf(stderr, "usage: quot [-cfhnv] [-a | filesystem ...]\n");
  482. #else /* COMPAT */
  483. fprintf(stderr, "usage: quot [-cfhknv] [-a | filesystem ...]\n");
  484. #endif /* COMPAT */
  485. exit(1);
  486. }
  487. void
  488. quot(char *name, char *mp)
  489. {
  490. int fd;
  491. struct fs *fs;
  492. get_inode(-1, NULL, 0); /* flush cache */
  493. inituser();
  494. initfsizes();
  495. if ((fd = open(name,0)) < 0) {
  496. warn("%s", name);
  497. close(fd);
  498. return;
  499. }
  500. switch (errno = sbget(fd, &fs, UFS_STDSB, UFS_NOCSUM)) {
  501. case 0:
  502. break;
  503. case ENOENT:
  504. warn("Cannot find file system superblock");
  505. close(fd);
  506. return;
  507. default:
  508. warn("Unable to read file system superblock");
  509. close(fd);
  510. return;
  511. }
  512. printf("%s:",name);
  513. if (mp)
  514. printf(" (%s)",mp);
  515. putchar('\n');
  516. (*func)(fd, fs, name);
  517. free(fs);
  518. close(fd);
  519. }
  520. int
  521. main(int argc, char *argv[])
  522. {
  523. char all = 0;
  524. struct statfs *mp;
  525. struct fstab *fs;
  526. int cnt;
  527. int ch;
  528. func = douser;
  529. #ifndef COMPAT
  530. header = getbsize(&headerlen,&blocksize);
  531. #endif
  532. while ((ch = getopt(argc, argv, "acfhknv")) != -1) {
  533. switch (ch) {
  534. case 'a':
  535. all = 1;
  536. break;
  537. case 'c':
  538. func = dofsizes;
  539. break;
  540. case 'f':
  541. count = 1;
  542. break;
  543. case 'h':
  544. estimate = 1;
  545. break;
  546. #ifndef COMPAT
  547. case 'k':
  548. blocksize = 1024;
  549. break;
  550. #endif /* COMPAT */
  551. case 'n':
  552. func = donames;
  553. break;
  554. case 'v':
  555. unused = 1;
  556. break;
  557. default:
  558. usage();
  559. }
  560. }
  561. argc -= optind;
  562. argv += optind;
  563. if ((argc == 0 && !all) || (all && argc))
  564. usage();
  565. if (all) {
  566. cnt = getmntinfo(&mp,MNT_NOWAIT);
  567. for (; --cnt >= 0; mp++) {
  568. if (!strncmp(mp->f_fstypename, "ufs", MFSNAMELEN))
  569. quot(mp->f_mntfromname, mp->f_mntonname);
  570. }
  571. }
  572. while (--argc >= 0) {
  573. if ((fs = getfsfile(*argv)) != NULL)
  574. quot(fs->fs_spec, 0);
  575. else
  576. quot(*argv,0);
  577. argv++;
  578. }
  579. return 0;
  580. }