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
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/disklabel.h>
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ffs/fs.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <fstab.h>
44 #include <inttypes.h>
45 #include <libufs.h>
46 #include <mntopts.h>
47 #include <paths.h>
48 #include <pwd.h>
49 #include <stdbool.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56
57 /* some flags of what to do: */
58 static bool all;
59 static bool count;
60 static bool noname;
61 static bool unused;
62 static void (*func)(int, struct fs *);
63 static long blocksize;
64 static char *header;
65 static int headerlen;
66
67 static union dinode *get_inode(int, struct fs *, ino_t);
68 static int isfree(struct fs *, union dinode *);
69 static void inituser(void);
70 static void usrrehash(void);
71 static struct user *user(uid_t);
72 static int cmpusers(const void *, const void *);
73 static void uses(uid_t, daddr_t, time_t);
74 static void initfsizes(void);
75 static void dofsizes(int, struct fs *);
76 static void douser(int, struct fs *);
77 static void donames(int, struct fs *);
78 static void usage(void);
79 static void quot(char *, char *);
80
81 /*
82 * Original BSD quot doesn't round to number of frags/blocks,
83 * doesn't account for indirection blocks and gets it totally
84 * wrong if the size is a multiple of the blocksize.
85 * The new code always counts the number of 512 byte blocks
86 * instead of the number of kilobytes and converts them to
87 * kByte when done (on request).
88 *
89 * Due to the size of modern disks, we must cast intermediate
90 * values to 64 bits to prevent potential overflows.
91 */
92 #define SIZE(n) ((int)(((intmax_t)(n) * 512 + blocksize - 1) / blocksize))
93
94 #define INOCNT(fs) ((fs)->fs_ipg)
95 #define INOSZ(fs) \
96 (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
97 sizeof(struct ufs2_dinode)) * INOCNT(fs))
98
99 #define DIP(fs, dp, field) \
100 (((fs)->fs_magic == FS_UFS1_MAGIC) ? \
101 (dp)->dp1.field : (dp)->dp2.field)
102
103 static union dinode *
get_inode(int fd,struct fs * super,ino_t ino)104 get_inode(int fd, struct fs *super, ino_t ino)
105 {
106 static union dinode *ipbuf;
107 static struct cg *cgp;
108 static ino_t last;
109 static unsigned long cg;
110 struct ufs2_dinode *di2;
111 off_t off;
112
113 if (fd < 0) { /* flush cache */
114 free(ipbuf);
115 ipbuf = NULL;
116 free(cgp);
117 cgp = NULL;
118 return (NULL);
119 }
120
121 if (ipbuf == NULL || ino < last || ino >= last + INOCNT(super)) {
122 if (super->fs_magic == FS_UFS2_MAGIC &&
123 (cgp == NULL || cg != ino_to_cg(super, ino))) {
124 cg = ino_to_cg(super, ino);
125 if (cgp == NULL && (cgp = malloc(super->fs_cgsize)) == NULL)
126 errx(1, "allocate cg");
127 if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0)
128 err(1, "lseek cg");
129 if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize)
130 err(1, "read cg");
131 if (!cg_chkmagic(cgp))
132 errx(1, "cg has bad magic");
133 }
134 if (ipbuf == NULL && (ipbuf = malloc(INOSZ(super))) == NULL)
135 errx(1, "allocate inodes");
136 last = rounddown(ino, INOCNT(super));
137 off = (off_t)ino_to_fsba(super, last) << super->fs_fshift;
138 if (lseek(fd, off, SEEK_SET) != off ||
139 read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
140 err(1, "read inodes");
141 }
142
143 if (super->fs_magic == FS_UFS1_MAGIC)
144 return ((union dinode *)
145 &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
146 di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)];
147 /* If the inode is unused, it might be unallocated too, so zero it. */
148 if (isclr(cg_inosused(cgp), ino % super->fs_ipg))
149 memset(di2, 0, sizeof(*di2));
150 return ((union dinode *)di2);
151 }
152
153 static int
isfree(struct fs * super,union dinode * dp)154 isfree(struct fs *super, union dinode *dp)
155 {
156 switch (DIP(super, dp, di_mode) & IFMT) {
157 case IFIFO:
158 case IFLNK: /* should check FASTSYMLINK? */
159 case IFDIR:
160 case IFREG:
161 return 0;
162 case IFCHR:
163 case IFBLK:
164 case IFSOCK:
165 case IFWHT:
166 case 0:
167 return 1;
168 default:
169 errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT);
170 }
171 }
172
173 static struct user {
174 uid_t uid;
175 char *name;
176 daddr_t space;
177 long count;
178 daddr_t spc30;
179 daddr_t spc60;
180 daddr_t spc90;
181 } *users;
182 static unsigned int nusers;
183
184 static void
inituser(void)185 inituser(void)
186 {
187 struct user *usr;
188 unsigned int i;
189
190 if (nusers == 0) {
191 nusers = 8;
192 if ((users = calloc(nusers, sizeof(*users))) == NULL)
193 errx(1, "allocate users");
194 } else {
195 for (usr = users, i = nusers; i-- > 0; usr++) {
196 usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
197 usr->count = 0;
198 }
199 }
200 }
201
202 static void
usrrehash(void)203 usrrehash(void)
204 {
205 struct user *usr, *usrn;
206 struct user *svusr;
207 unsigned int i;
208
209 svusr = users;
210 nusers *= 2;
211 if ((users = calloc(nusers, sizeof(*users))) == NULL)
212 errx(1, "allocate users");
213 for (usr = svusr, i = nusers / 2; i-- > 0; usr++) {
214 for (usrn = users + usr->uid % nusers; usrn->name; usrn--) {
215 if (usrn <= users)
216 usrn += nusers;
217 }
218 *usrn = *usr;
219 }
220 }
221
222 static struct user *
user(uid_t uid)223 user(uid_t uid)
224 {
225 struct user *usr;
226 struct passwd *pwd;
227 unsigned int i;
228
229 while (1) {
230 for (usr = users + uid % nusers, i = nusers; i-- > 0; usr--) {
231 if (usr->name == NULL) {
232 usr->uid = uid;
233 if (noname || (pwd = getpwuid(uid)) == NULL)
234 asprintf(&usr->name, "#%u", uid);
235 else
236 usr->name = strdup(pwd->pw_name);
237 if (usr->name == NULL)
238 errx(1, "allocate users");
239 }
240 if (usr->uid == uid)
241 return (usr);
242 if (usr <= users)
243 usr += nusers;
244 }
245 usrrehash();
246 }
247 }
248
249 static int
cmpusers(const void * v1,const void * v2)250 cmpusers(const void *v1, const void *v2)
251 {
252 const struct user *u1 = v1, *u2 = v2;
253
254 return (u2->space > u1->space ? 1 :
255 u2->space < u1->space ? -1 :
256 u1->uid > u2->uid ? 1 :
257 u1->uid < u2->uid ? -1 : 0);
258 }
259
260 #define sortusers(users) \
261 qsort((users), nusers, sizeof(struct user), cmpusers)
262
263 static void
uses(uid_t uid,daddr_t blks,time_t act)264 uses(uid_t uid, daddr_t blks, time_t act)
265 {
266 static time_t today;
267 struct user *usr;
268
269 if (!today)
270 time(&today);
271
272 usr = user(uid);
273 usr->count++;
274 usr->space += blks;
275
276 if (today - act > 90L * 24L * 60L * 60L)
277 usr->spc90 += blks;
278 if (today - act > 60L * 24L * 60L * 60L)
279 usr->spc60 += blks;
280 if (today - act > 30L * 24L * 60L * 60L)
281 usr->spc30 += blks;
282 }
283
284 #define FSZCNT 512U
285 static struct fsizes {
286 struct fsizes *fsz_next;
287 daddr_t fsz_first, fsz_last;
288 ino_t fsz_count[FSZCNT];
289 daddr_t fsz_sz[FSZCNT];
290 } *fsizes;
291
292 static void
initfsizes(void)293 initfsizes(void)
294 {
295 struct fsizes *fp;
296 unsigned int i;
297
298 for (fp = fsizes; fp; fp = fp->fsz_next) {
299 for (i = FSZCNT; i-- > 0;) {
300 fp->fsz_count[i] = 0;
301 fp->fsz_sz[i] = 0;
302 }
303 }
304 }
305
306 static void
dofsizes(int fd,struct fs * super)307 dofsizes(int fd, struct fs *super)
308 {
309 ino_t inode, maxino;
310 union dinode *dp;
311 daddr_t sz, ksz;
312 struct fsizes *fp, **fsp;
313 unsigned int i;
314
315 maxino = super->fs_ncg * super->fs_ipg - 1;
316 for (inode = 0; inode < maxino; inode++) {
317 if ((dp = get_inode(fd, super, inode)) != NULL &&
318 !isfree(super, dp)) {
319 sz = DIP(super, dp, di_blocks);
320 ksz = SIZE(sz);
321 for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) {
322 if (ksz < fp->fsz_last)
323 break;
324 }
325 if (fp == NULL || ksz < fp->fsz_first) {
326 if ((fp = malloc(sizeof(*fp))) == NULL)
327 errx(1, "allocate fsize structure");
328 fp->fsz_next = *fsp;
329 *fsp = fp;
330 fp->fsz_first = rounddown(ksz, FSZCNT);
331 fp->fsz_last = fp->fsz_first + FSZCNT;
332 for (i = FSZCNT; i-- > 0;) {
333 fp->fsz_count[i] = 0;
334 fp->fsz_sz[i] = 0;
335 }
336 }
337 fp->fsz_count[ksz % FSZCNT]++;
338 fp->fsz_sz[ksz % FSZCNT] += sz;
339 }
340 }
341 sz = 0;
342 for (fp = fsizes; fp != NULL; fp = fp->fsz_next) {
343 for (i = 0; i < FSZCNT; i++) {
344 if (fp->fsz_count[i] != 0) {
345 printf("%jd\t%jd\t%d\n",
346 (intmax_t)(fp->fsz_first + i),
347 (intmax_t)fp->fsz_count[i],
348 SIZE(sz += fp->fsz_sz[i]));
349 }
350 }
351 }
352 }
353
354 static void
douser(int fd,struct fs * super)355 douser(int fd, struct fs *super)
356 {
357 ino_t inode, maxino;
358 struct user *usr, *usrs;
359 union dinode *dp;
360 int n;
361
362 maxino = super->fs_ncg * super->fs_ipg - 1;
363 for (inode = 0; inode < maxino; inode++) {
364 if ((dp = get_inode(fd, super, inode)) != NULL &&
365 !isfree(super, dp)) {
366 uses(DIP(super, dp, di_uid),
367 DIP(super, dp, di_blocks),
368 DIP(super, dp, di_atime));
369 }
370 }
371 if ((usrs = malloc(nusers * sizeof(*usrs))) == NULL)
372 errx(1, "allocate users");
373 memcpy(usrs, users, nusers * sizeof(*usrs));
374 sortusers(usrs);
375 for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
376 printf("%5d", SIZE(usr->space));
377 if (count)
378 printf("\t%5ld", usr->count);
379 printf("\t%-8s", usr->name);
380 if (unused) {
381 printf("\t%5d\t%5d\t%5d",
382 SIZE(usr->spc30),
383 SIZE(usr->spc60),
384 SIZE(usr->spc90));
385 }
386 printf("\n");
387 }
388 free(usrs);
389 }
390
391 static void
donames(int fd,struct fs * super)392 donames(int fd, struct fs *super)
393 {
394 union dinode *dp;
395 char *end, *line;
396 size_t cap;
397 ssize_t len;
398 intmax_t inode, maxino;
399
400 maxino = super->fs_ncg * super->fs_ipg - 1;
401 line = NULL;
402 cap = 0;
403 while ((len = getline(&line, &cap, stdin)) > 0) {
404 if (len > 0 && line[len - 1] == '\n')
405 line[--len] = '\0';
406 inode = strtoimax(line, &end, 10);
407 /*
408 * Silently ignore lines that do not begin with a number.
409 * For backward compatibility reasons, we do not require
410 * the optional comment to be preceded by whitespace.
411 */
412 if (end == line)
413 continue;
414 if (inode <= 0 || inode > maxino) {
415 warnx("invalid inode %jd", inode);
416 continue;
417 }
418 if ((dp = get_inode(fd, super, inode)) != NULL &&
419 !isfree(super, dp)) {
420 printf("%s\t", user(DIP(super, dp, di_uid))->name);
421 /* now skip whitespace */
422 while (*end == ' ' || *end == '\t')
423 end++;
424 /* and print out the remainder of the input line */
425 printf("%s\n", end);
426 } else {
427 /* skip this line */
428 }
429 }
430 free(line);
431 }
432
433 static void
usage(void)434 usage(void)
435 {
436 fprintf(stderr, "usage: quot [-cfknv] [-a | filesystem ...]\n");
437 exit(1);
438 }
439
440 void
quot(char * name,char * mp)441 quot(char *name, char *mp)
442 {
443 int fd;
444 struct fs *fs;
445
446 get_inode(-1, NULL, 0); /* flush cache */
447 inituser();
448 initfsizes();
449 if ((fd = open(name, 0)) < 0) {
450 warn("%s", name);
451 close(fd);
452 return;
453 }
454 switch (errno = sbget(fd, &fs, UFS_STDSB, UFS_NOCSUM)) {
455 case 0:
456 break;
457 case ENOENT:
458 warn("Cannot find file system superblock");
459 close(fd);
460 return;
461 default:
462 warn("Unable to read file system superblock");
463 close(fd);
464 return;
465 }
466 printf("%s:", name);
467 if (mp)
468 printf(" (%s)", mp);
469 putchar('\n');
470 (*func)(fd, fs);
471 free(fs);
472 close(fd);
473 }
474
475 int
main(int argc,char * argv[])476 main(int argc, char *argv[])
477 {
478 struct statfs *mp;
479 int ch, cnt;
480
481 func = douser;
482 header = getbsize(&headerlen, &blocksize);
483 while ((ch = getopt(argc, argv, "acfhkNnv")) != -1) {
484 switch (ch) {
485 case 'a':
486 all = true;
487 break;
488 case 'c':
489 func = dofsizes;
490 break;
491 case 'f':
492 count = true;
493 break;
494 case 'h':
495 /* ignored for backward compatibility */
496 break;
497 case 'k':
498 blocksize = 1024;
499 break;
500 case 'N':
501 noname = true;
502 break;
503 case 'n':
504 func = donames;
505 break;
506 case 'v':
507 unused = true;
508 break;
509 default:
510 usage();
511 }
512 }
513 argc -= optind;
514 argv += optind;
515
516 if ((argc == 0 && !all) || (all && argc))
517 usage();
518
519 if (all) {
520 for (cnt = getmntinfo(&mp, MNT_NOWAIT); --cnt >= 0; mp++)
521 if (strncmp(mp->f_fstypename, "ufs", MFSNAMELEN) == 0)
522 quot(mp->f_mntfromname, mp->f_mntonname);
523 }
524 while (argc-- > 0) {
525 if ((mp = getmntpoint(*argv)) != NULL)
526 quot(mp->f_mntfromname, mp->f_mntonname);
527 else
528 quot(*argv, 0);
529 argv++;
530 }
531 return (0);
532 }
533