1/* $OpenBSD: quotacheck.c,v 1.42 2024/02/03 18:51:57 beck Exp $ */ 2/* $NetBSD: quotacheck.c,v 1.12 1996/03/30 22:34:25 mark Exp $ */ 3 4/* 5 * Copyright (c) 1980, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Robert Elz at The University of Melbourne. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36/* 37 * Fix up / report on disk quotas & usage 38 */ 39#include <sys/param.h> /* DEV_BSIZE MAXBSIZE */ 40#include <sys/stat.h> 41#include <sys/wait.h> 42 43#include <ufs/ufs/dinode.h> 44#include <ufs/ufs/quota.h> 45#include <ufs/ffs/fs.h> 46 47#include <fcntl.h> 48#include <fstab.h> 49#include <pwd.h> 50#include <grp.h> 51#include <errno.h> 52#include <unistd.h> 53#include <limits.h> 54#include <util.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> 58#include <err.h> 59#include "fsutil.h" 60 61char *qfname = QUOTAFILENAME; 62char *qfextension[] = INITQFNAMES; 63char *quotagroup = QUOTAGROUP; 64 65union { 66 struct fs sblk; 67 char dummy[MAXBSIZE]; 68} sb_un; 69#define sblock sb_un.sblk 70union { 71 struct cg cgblk; 72 char dummy[MAXBSIZE]; 73} cg_un; 74#define cgblk cg_un.cgblk 75 76long maxino; 77 78union dinode { 79 struct ufs1_dinode dp1; 80 struct ufs2_dinode dp2; 81}; 82#define DIP(dp, field) \ 83 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 84 (dp)->dp1.field : (dp)->dp2.field) 85 86struct quotaname { 87 long flags; 88 char grpqfname[PATH_MAX + 1]; 89 char usrqfname[PATH_MAX + 1]; 90}; 91#define HASUSR 1 92#define HASGRP 2 93 94struct fileusage { 95 struct fileusage *fu_next; 96 u_int32_t fu_curinodes; 97 u_int32_t fu_curblocks; 98 u_int32_t fu_id; /* uid_t or gid_t */ 99 char fu_name[1]; 100 /* actually bigger */ 101}; 102#define FUHASH 1024 /* must be power of two */ 103struct fileusage *fuhead[MAXQUOTAS][FUHASH]; 104 105int gflag; /* check group quotas */ 106int uflag; /* check user quotas */ 107int flags; /* check flags (avd) */ 108int fi; /* open disk file descriptor */ 109u_int32_t highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */ 110 111struct fileusage * 112 addid(u_int32_t, int, char *); 113char *blockcheck(char *); 114void bread(daddr_t, char *, long); 115int chkquota(const char *, const char *, const char *, void *, pid_t *); 116void freeinodebuf(void); 117union dinode * 118 getnextinode(ino_t); 119int getquotagid(void); 120int hasquota(struct fstab *, int, char **); 121struct fileusage * 122 lookup(u_int32_t, int); 123void *needchk(struct fstab *); 124int oneof_realpath(char *, char*[], int); 125int oneof_specname(char *, char*[], int); 126void setinodebuf(ino_t); 127int update(const char *, const char *, int); 128void usage(void); 129 130int 131main(int argc, char *argv[]) 132{ 133 struct fstab *fs; 134 struct passwd *pw; 135 struct group *gr; 136 struct quotaname *auxdata; 137 int i, argnum, maxrun, errs, ch; 138 u_int64_t done = 0; /* XXX supports maximum 64 filesystems */ 139 const char *errstr; 140 char *name; 141 142 checkroot(); 143 144 errs = maxrun = 0; 145 while ((ch = getopt(argc, argv, "adguvl:")) != -1) { 146 switch(ch) { 147 case 'a': 148 flags |= CHECK_PREEN; 149 break; 150 case 'd': 151 flags |= CHECK_DEBUG; 152 break; 153 case 'g': 154 gflag = 1; 155 break; 156 case 'l': 157 maxrun = strtonum(optarg, 0, INT_MAX, &errstr); 158 if (errstr) 159 errx(1, "-l %s: %s", optarg, errstr); 160 break; 161 case 'u': 162 uflag = 1; 163 break; 164 case 'v': 165 flags |= CHECK_VERBOSE; 166 break; 167 default: 168 usage(); 169 } 170 } 171 argc -= optind; 172 argv += optind; 173 if ((argc == 0 && !(flags&CHECK_PREEN)) || 174 (argc > 0 && (flags&CHECK_PREEN))) 175 usage(); 176 if (!gflag && !uflag) { 177 gflag++; 178 uflag++; 179 } 180 if (gflag) { 181 setgrent(); 182 while ((gr = getgrent()) != 0) 183 (void) addid(gr->gr_gid, GRPQUOTA, gr->gr_name); 184 endgrent(); 185 } 186 if (uflag) { 187 setpwent(); 188 while ((pw = getpwent()) != 0) 189 (void) addid(pw->pw_uid, USRQUOTA, pw->pw_name); 190 endpwent(); 191 } 192 if (flags&CHECK_PREEN) 193 exit(checkfstab(flags, maxrun, needchk, chkquota)); 194 if (setfsent() == 0) 195 err(1, "%s: can't open", _PATH_FSTAB); 196 while ((fs = getfsent()) != NULL) { 197 if (((argnum = oneof_realpath(fs->fs_file, argv, argc)) >= 0 || 198 (argnum = oneof_specname(fs->fs_spec, argv, argc)) >= 0) && 199 (auxdata = needchk(fs)) && 200 (name = blockcheck(fs->fs_spec))) { 201 done |= 1 << argnum; 202 errs += chkquota(fs->fs_vfstype, name, 203 fs->fs_file, auxdata, NULL); 204 } 205 } 206 endfsent(); 207 for (i = 0; i < argc; i++) 208 if ((done & (1 << i)) == 0) 209 fprintf(stderr, "%s not found in %s\n", 210 argv[i], _PATH_FSTAB); 211 exit(errs); 212} 213 214void 215usage(void) 216{ 217 extern char *__progname; 218 (void)fprintf(stderr, "usage: %s [-adguv] [-l maxparallel] " 219 "filesystem ...\n", __progname); 220 exit(1); 221} 222 223void * 224needchk(struct fstab *fs) 225{ 226 struct quotaname *qnp; 227 char *qfnp; 228 229 if (fs->fs_passno == 0) 230 return NULL; 231 if (strcmp(fs->fs_type, FSTAB_RW)) 232 return (NULL); 233 if (strcmp(fs->fs_vfstype, "ffs") && 234 strcmp(fs->fs_vfstype, "ufs") && 235 strcmp(fs->fs_vfstype, "mfs")) 236 return (NULL); 237 if ((qnp = malloc(sizeof(*qnp))) == NULL) 238 err(1, "%s", strerror(errno)); 239 qnp->flags = 0; 240 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) { 241 strlcpy(qnp->grpqfname, qfnp, sizeof qnp->grpqfname); 242 qnp->flags |= HASGRP; 243 } 244 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) { 245 strlcpy(qnp->usrqfname, qfnp, sizeof qnp->usrqfname); 246 qnp->flags |= HASUSR; 247 } 248 if (qnp->flags) 249 return (qnp); 250 free(qnp); 251 return (NULL); 252} 253 254/* 255 * Possible superblock locations ordered from most to least likely. 256 */ 257static int sblock_try[] = SBLOCKSEARCH; 258 259/* 260 * Scan the specified file system to check quota(s) present on it. 261 */ 262int 263chkquota(const char *vfstype, const char *fsname, const char *mntpt, 264 void *auxarg, pid_t *pidp) 265{ 266 struct quotaname *qnp = auxarg; 267 struct fileusage *fup; 268 union dinode *dp; 269 int cg, i, mode, errs = 0, status; 270 ino_t ino, inosused; 271 pid_t pid; 272 char *cp; 273 274 switch (pid = fork()) { 275 case -1: /* error */ 276 warn("fork"); 277 return 1; 278 case 0: /* child */ 279 if ((fi = opendev(fsname, O_RDONLY, 0, NULL)) == -1) 280 err(1, "%s", fsname); 281 sync(); 282 for (i = 0; sblock_try[i] != -1; i++) { 283 bread(sblock_try[i] / DEV_BSIZE, (char *)&sblock, 284 (long)SBLOCKSIZE); 285 if ((sblock.fs_magic == FS_UFS1_MAGIC || 286 (sblock.fs_magic == FS_UFS2_MAGIC && 287 sblock.fs_sblockloc == sblock_try[i])) && 288 sblock.fs_bsize <= MAXBSIZE && 289 sblock.fs_bsize >= sizeof(struct fs)) 290 break; 291 } 292 if (sblock_try[i] == -1) { 293 warn("Cannot find file system superblock"); 294 return (1); 295 } 296 maxino = sblock.fs_ncg * sblock.fs_ipg; 297 for (cg = 0; cg < sblock.fs_ncg; cg++) { 298 ino = cg * sblock.fs_ipg; 299 setinodebuf(ino); 300 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), 301 (char *)(&cgblk), sblock.fs_cgsize); 302 if (sblock.fs_magic == FS_UFS2_MAGIC) 303 inosused = cgblk.cg_initediblk; 304 else 305 inosused = sblock.fs_ipg; 306 for (i = 0; i < inosused; i++, ino++) { 307 if ((dp = getnextinode(ino)) == NULL || 308 ino < ROOTINO || 309 (mode = DIP(dp, di_mode) & IFMT) == 0) 310 continue; 311 if (qnp->flags & HASGRP) { 312 fup = addid(DIP(dp, di_gid), 313 GRPQUOTA, NULL); 314 fup->fu_curinodes++; 315 if (mode == IFREG || mode == IFDIR || 316 mode == IFLNK) 317 fup->fu_curblocks += 318 DIP(dp, di_blocks); 319 } 320 if (qnp->flags & HASUSR) { 321 fup = addid(DIP(dp, di_uid), 322 USRQUOTA, NULL); 323 fup->fu_curinodes++; 324 if (mode == IFREG || mode == IFDIR || 325 mode == IFLNK) 326 fup->fu_curblocks += 327 DIP(dp, di_blocks); 328 } 329 } 330 } 331 freeinodebuf(); 332 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) { 333 (void)printf("*** Checking "); 334 if (qnp->flags & HASUSR) { 335 (void)printf("%s", qfextension[USRQUOTA]); 336 if (qnp->flags & HASGRP) 337 (void)printf(" and "); 338 } 339 if (qnp->flags & HASGRP) 340 (void)printf("%s", qfextension[GRPQUOTA]); 341 (void)printf(" quotas for %s (%s), %swait\n", 342 fsname, mntpt, pidp? "no" : ""); 343 } 344 if (qnp->flags & HASUSR) 345 errs += update(mntpt, qnp->usrqfname, USRQUOTA); 346 if (qnp->flags & HASGRP) 347 errs += update(mntpt, qnp->grpqfname, GRPQUOTA); 348 close(fi); 349 exit (errs); 350 break; 351 default: /* parent */ 352 if (pidp != NULL) { 353 *pidp = pid; 354 return 0; 355 } 356 if (waitpid(pid, &status, 0) == -1) { 357 warn("waitpid"); 358 return 1; 359 } 360 if (WIFEXITED(status)) { 361 if (WEXITSTATUS(status) != 0) 362 return WEXITSTATUS(status); 363 } else if (WIFSIGNALED(status)) { 364 warnx("%s: %s", fsname, strsignal(WTERMSIG(status))); 365 return 1; 366 } 367 break; 368 } 369 return (0); 370} 371 372/* 373 * Update a specified quota file. 374 */ 375int 376update(const char *fsname, const char *quotafile, int type) 377{ 378 struct fileusage *fup; 379 FILE *qfi, *qfo; 380 u_int32_t id, lastid; 381 struct dqblk dqbuf; 382 static int warned = 0; 383 static struct dqblk zerodqbuf; 384 static struct fileusage zerofileusage; 385 386 if (flags&CHECK_DEBUG) 387 printf("updating: %s\n", quotafile); 388 389 if ((qfo = fopen(quotafile, (flags&CHECK_DEBUG)? "r" : "r+")) == NULL) { 390 if (errno == ENOENT) 391 qfo = fopen(quotafile, "w+"); 392 if (qfo) { 393 warnx("creating quota file: %s", quotafile); 394#define MODE (S_IRUSR|S_IWUSR|S_IRGRP) 395 (void) fchown(fileno(qfo), getuid(), getquotagid()); 396 (void) fchmod(fileno(qfo), MODE); 397 } else { 398 warn("%s", quotafile); 399 return (1); 400 } 401 } 402 if ((qfi = fopen(quotafile, "r")) == NULL) { 403 warn("%s", quotafile); 404 (void) fclose(qfo); 405 return (1); 406 } 407 if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (caddr_t)0) == -1 && 408 errno == EOPNOTSUPP && !warned && 409 (flags&(CHECK_DEBUG|CHECK_VERBOSE))) { 410 warned++; 411 (void)printf("*** Warning: %s\n", 412 "Quotas are not compiled into this kernel"); 413 } 414 for (lastid = highid[type], id = 0; id <= lastid; id++) { 415 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0) 416 dqbuf = zerodqbuf; 417 if ((fup = lookup(id, type)) == 0) 418 fup = &zerofileusage; 419 if (dqbuf.dqb_curinodes == fup->fu_curinodes && 420 dqbuf.dqb_curblocks == fup->fu_curblocks) { 421 fup->fu_curinodes = 0; 422 fup->fu_curblocks = 0; 423 fseek(qfo, (long)sizeof(struct dqblk), SEEK_CUR); 424 continue; 425 } 426 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) { 427 if (flags&CHECK_PREEN) 428 printf("%s: ", fsname); 429 printf("%-8s fixed:", fup->fu_name); 430 if (dqbuf.dqb_curinodes != fup->fu_curinodes) 431 (void)printf("\tinodes %d -> %u", 432 dqbuf.dqb_curinodes, fup->fu_curinodes); 433 if (dqbuf.dqb_curblocks != fup->fu_curblocks) 434 (void)printf("\tblocks %u -> %u", 435 dqbuf.dqb_curblocks, fup->fu_curblocks); 436 (void)printf("\n"); 437 } 438 /* 439 * Reset time limit if have a soft limit and were 440 * previously under it, but are now over it. 441 */ 442 if (dqbuf.dqb_bsoftlimit && 443 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 444 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit) 445 dqbuf.dqb_btime = 0; 446 if (dqbuf.dqb_isoftlimit && 447 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit && 448 fup->fu_curblocks >= dqbuf.dqb_isoftlimit) 449 dqbuf.dqb_itime = 0; 450 dqbuf.dqb_curinodes = fup->fu_curinodes; 451 dqbuf.dqb_curblocks = fup->fu_curblocks; 452 if (!(flags & CHECK_DEBUG)) { 453 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); 454 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, 455 (caddr_t)&dqbuf); 456 } 457 fup->fu_curinodes = 0; 458 fup->fu_curblocks = 0; 459 } 460 fclose(qfi); 461 fflush(qfo); 462 if (!(flags & CHECK_DEBUG)) 463 ftruncate(fileno(qfo), 464 (off_t)((highid[type] + 1) * sizeof(struct dqblk))); 465 fclose(qfo); 466 return (0); 467} 468 469/* 470 * Check to see if realpath(target) matches a realpath() in list of size cnt. 471 */ 472int 473oneof_realpath(char *target, char *list[], int cnt) 474{ 475 int i; 476 char realtarget[PATH_MAX], realargv[PATH_MAX]; 477 char *rv; 478 479 rv = realpath(target, realtarget); 480 if (rv == NULL) 481 return (-1); 482 483 for (i = 0; i < cnt; i++) { 484 rv = realpath(list[i], realargv); 485 if (rv && strcmp(realtarget, realargv) == 0) 486 break; 487 } 488 489 if (i < cnt) 490 return (i); 491 else 492 return (-1); 493} 494 495/* 496 * Check to see if opendev(target) matches a opendev() in list of size cnt. 497 */ 498int 499oneof_specname(char *target, char *list[], int cnt) 500{ 501 int i, fd; 502 char *tmp, *targetdev, *argvdev; 503 504 fd = opendev(target, O_RDONLY, 0, &tmp); 505 if (fd == -1) 506 return (-1); 507 close(fd); 508 targetdev = strdup(tmp); 509 510 for (i = 0; i < cnt; i++) { 511 fd = opendev(list[i], O_RDONLY, 0, &argvdev); 512 if (fd == -1) 513 continue; 514 close(fd); 515 if (strcmp(targetdev, argvdev) == 0) 516 break; 517 } 518 519 free(targetdev); 520 521 if (i < cnt) 522 return (i); 523 else 524 return (-1); 525} 526 527/* 528 * Determine the group identifier for quota files. 529 */ 530int 531getquotagid(void) 532{ 533 struct group *gr; 534 535 if ((gr = getgrnam(quotagroup)) != NULL) 536 return (gr->gr_gid); 537 return (-1); 538} 539 540/* 541 * Check to see if a particular quota is to be enabled. 542 */ 543int 544hasquota(struct fstab *fs, int type, char **qfnamep) 545{ 546 char *opt, *cp; 547 static char initname, usrname[100], grpname[100]; 548 static char buf[BUFSIZ]; 549 550 if (!initname) { 551 (void)snprintf(usrname, sizeof(usrname), 552 "%s%s", qfextension[USRQUOTA], qfname); 553 (void)snprintf(grpname, sizeof(grpname), 554 "%s%s", qfextension[GRPQUOTA], qfname); 555 initname = 1; 556 } 557 (void)strlcpy(buf, fs->fs_mntops, sizeof(buf)); 558 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 559 if ((cp = strchr(opt, '=')) != NULL) 560 *cp++ = '\0'; 561 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 562 break; 563 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 564 break; 565 } 566 if (!opt) 567 return (0); 568 if (cp) 569 *qfnamep = cp; 570 else { 571 (void)snprintf(buf, sizeof(buf), 572 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 573 *qfnamep = buf; 574 } 575 return (1); 576} 577 578/* 579 * Routines to manage the file usage table. 580 * 581 * Lookup an id of a specific type. 582 */ 583struct fileusage * 584lookup(u_int32_t id, int type) 585{ 586 struct fileusage *fup; 587 588 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next) 589 if (fup->fu_id == id) 590 return (fup); 591 return (NULL); 592} 593 594/* 595 * Add a new file usage id if it does not already exist. 596 */ 597struct fileusage * 598addid(u_int32_t id, int type, char *name) 599{ 600 struct fileusage *fup, **fhp; 601 int len; 602 603 if ((fup = lookup(id, type)) != NULL) 604 return (fup); 605 if (name) 606 len = strlen(name); 607 else 608 len = 10; 609 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL) 610 err(1, "%s", strerror(errno)); 611 fhp = &fuhead[type][id & (FUHASH - 1)]; 612 fup->fu_next = *fhp; 613 *fhp = fup; 614 fup->fu_id = id; 615 if (id > highid[type]) 616 highid[type] = id; 617 if (name) 618 memcpy(fup->fu_name, name, len + 1); 619 else 620 (void)snprintf(fup->fu_name, len, "%u", id); 621 return (fup); 622} 623 624/* 625 * Special purpose version of ginode used to optimize pass 626 * over all the inodes in numerical order. 627 */ 628static ino_t nextino, lastinum, lastvalidinum; 629static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 630static caddr_t inodebuf; 631#define INOBUFSIZE 56*1024 /* size of buffer to read inodes */ 632 633union dinode * 634getnextinode(ino_t inumber) 635{ 636 long size; 637 daddr_t dblk; 638 union dinode *dp; 639 static caddr_t nextinop; 640 641 if (inumber != nextino++ || inumber > lastvalidinum) 642 err(1, "bad inode number %llu to nextinode", 643 (unsigned long long)inumber); 644 if (inumber >= lastinum) { 645 readcnt++; 646 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum)); 647 if (readcnt % readpercg == 0) { 648 size = partialsize; 649 lastinum += partialcnt; 650 } else { 651 size = inobufsize; 652 lastinum += fullcnt; 653 } 654 /* 655 * If bread returns an error, it will already have zeroed 656 * out the buffer, so we do not need to do so here. 657 */ 658 bread(dblk, inodebuf, size); 659 nextinop = inodebuf; 660 } 661 dp = (union dinode *)nextinop; 662 if (sblock.fs_magic == FS_UFS1_MAGIC) 663 nextinop += sizeof(struct ufs1_dinode); 664 else 665 nextinop += sizeof(struct ufs2_dinode); 666 return (dp); 667} 668 669/* 670 * Prepare to scan a set of inodes. 671 */ 672void 673setinodebuf(ino_t inum) 674{ 675 676 if (inum % sblock.fs_ipg != 0) 677 errx(1, "bad inode number %llu to setinodebuf", 678 (unsigned long long)inum); 679 lastvalidinum = inum + sblock.fs_ipg - 1; 680 nextino = inum; 681 lastinum = inum; 682 readcnt = 0; 683 if (inodebuf != NULL) 684 return; 685 inobufsize = blkroundup(&sblock, INOBUFSIZE); 686 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 687 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 688 readpercg = sblock.fs_ipg / fullcnt; 689 partialcnt = sblock.fs_ipg % fullcnt; 690 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 691 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 692 if (partialcnt != 0) { 693 readpercg++; 694 } else { 695 partialcnt = fullcnt; 696 partialsize = inobufsize; 697 } 698 if ((inodebuf = malloc((size_t)inobufsize)) == NULL) 699 errx(1, "cannot allocate space for inode buffer"); 700} 701 702/* 703 * Free up data structures used to scan inodes. 704 */ 705void 706freeinodebuf(void) 707{ 708 709 free(inodebuf); 710 inodebuf = NULL; 711} 712 713/* 714 * Read specified disk blocks. 715 */ 716void 717bread(daddr_t bno, char *buf, long cnt) 718{ 719 if (pread(fi, buf, cnt, bno * DEV_BSIZE) != cnt) 720 err(1, "read failed on block %lld", (long long)bno); 721} 722