utilities.c revision 1.70 1 /* $NetBSD: utilities.c,v 1.70 2023/07/04 20:40:53 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
36 #else
37 __RCSID("$NetBSD: utilities.c,v 1.70 2023/07/04 20:40:53 riastradh Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47 #include <ufs/ffs/ffs_extern.h>
48 #include <ufs/ufs/ufs_bswap.h>
49 #include <ufs/ufs/quota2.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <signal.h>
59
60 #include "fsutil.h"
61 #include "fsck.h"
62 #include "extern.h"
63 #include "exitvalues.h"
64
65 long diskreads, totalreads; /* Disk cache statistics */
66
67 static void rwerror(const char *, daddr_t);
68
69 int
70 ftypeok(union dinode *dp)
71 {
72 switch (iswap16(DIP(dp, mode)) & IFMT) {
73
74 case IFDIR:
75 case IFREG:
76 case IFBLK:
77 case IFCHR:
78 case IFLNK:
79 case IFSOCK:
80 case IFIFO:
81 return (1);
82
83 default:
84 if (debug)
85 printf("bad file type 0%o\n", iswap16(DIP(dp, mode)));
86 return (0);
87 }
88 }
89
90 int
91 reply(const char *question)
92 {
93 int persevere;
94 char c;
95
96 if (preen)
97 pfatal("INTERNAL ERROR: GOT TO reply()");
98 persevere = !strcmp(question, "CONTINUE");
99 printf("\n");
100 if (!persevere && (nflag || fswritefd < 0)) {
101 printf("%s? no\n\n", question);
102 resolved = 0;
103 return (0);
104 }
105 if (yflag || (persevere && nflag)) {
106 printf("%s? yes\n\n", question);
107 return (1);
108 }
109 do {
110 printf("%s? [yn] ", question);
111 (void) fflush(stdout);
112 c = getc(stdin);
113 while (c != '\n' && getc(stdin) != '\n') {
114 if (feof(stdin)) {
115 resolved = 0;
116 return (0);
117 }
118 }
119 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
120 printf("\n");
121 if (c == 'y' || c == 'Y')
122 return (1);
123 resolved = 0;
124 return (0);
125 }
126
127 /*
128 * Malloc buffers and set up cache.
129 */
130 void
131 bufinit(void)
132 {
133 struct bufarea *bp;
134 long bufcnt, i;
135 char *bufp;
136
137 pbp = pdirbp = (struct bufarea *)0;
138 __CTASSERT(powerof2(DEV_BSIZE));
139 bufp = aligned_alloc(DEV_BSIZE,
140 roundup2((unsigned int)sblock->fs_bsize, DEV_BSIZE));
141 if (bufp == 0)
142 errexit("cannot allocate buffer pool");
143 cgblk.b_un.b_buf = bufp;
144 initbarea(&cgblk);
145 #ifndef NO_APPLE_UFS
146 __CTASSERT((APPLEUFS_LABEL_SIZE % DEV_BSIZE) == 0);
147 bufp = aligned_alloc(DEV_BSIZE, (unsigned int)APPLEUFS_LABEL_SIZE);
148 if (bufp == 0)
149 errexit("cannot allocate buffer pool");
150 appleufsblk.b_un.b_buf = bufp;
151 initbarea(&appleufsblk);
152 #endif
153 bufhead.b_next = bufhead.b_prev = &bufhead;
154 bufcnt = MAXBUFSPACE / sblock->fs_bsize;
155 if (bufcnt < MINBUFS)
156 bufcnt = MINBUFS;
157 for (i = 0; i < bufcnt; i++) {
158 bp = malloc(sizeof(struct bufarea));
159 __CTASSERT(powerof2(DEV_BSIZE));
160 bufp = aligned_alloc(DEV_BSIZE,
161 roundup2((unsigned int)sblock->fs_bsize, DEV_BSIZE));
162 if (bp == NULL || bufp == NULL) {
163 if (i >= MINBUFS) {
164 if (bp)
165 free(bp);
166 if (bufp)
167 free(bufp);
168 break;
169 }
170 errexit("cannot allocate buffer pool");
171 }
172 bp->b_un.b_buf = bufp;
173 bp->b_prev = &bufhead;
174 bp->b_next = bufhead.b_next;
175 bufhead.b_next->b_prev = bp;
176 bufhead.b_next = bp;
177 initbarea(bp);
178 }
179 bufhead.b_size = i; /* save number of buffers */
180 }
181
182 /*
183 * Manage a cache of directory blocks.
184 */
185 struct bufarea *
186 getdatablk(daddr_t blkno, long size)
187 {
188 struct bufarea *bp;
189
190 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
191 if (bp->b_bno == FFS_FSBTODB(sblock, blkno))
192 goto foundit;
193 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
194 if ((bp->b_flags & B_INUSE) == 0)
195 break;
196 if (bp == &bufhead)
197 errexit("deadlocked buffer pool");
198 /* fall through */
199 foundit:
200 getblk(bp, blkno, size);
201 bp->b_prev->b_next = bp->b_next;
202 bp->b_next->b_prev = bp->b_prev;
203 bp->b_prev = &bufhead;
204 bp->b_next = bufhead.b_next;
205 bufhead.b_next->b_prev = bp;
206 bufhead.b_next = bp;
207 bp->b_flags |= B_INUSE;
208 return (bp);
209 }
210
211 void
212 getblk(struct bufarea *bp, daddr_t blk, long size)
213 {
214 daddr_t dblk;
215
216 dblk = FFS_FSBTODB(sblock, blk);
217 totalreads++;
218 if (bp->b_bno != dblk) {
219 flush(fswritefd, bp);
220 diskreads++;
221 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
222 bp->b_bno = dblk;
223 bp->b_size = size;
224 }
225 }
226
227 void
228 flush(int fd, struct bufarea *bp)
229 {
230 int i, j;
231 struct csum *ccsp;
232
233 if (!bp->b_dirty)
234 return;
235 if (bp->b_errs != 0)
236 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
237 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
238 (long long)bp->b_bno);
239 bp->b_dirty = 0;
240 bp->b_errs = 0;
241 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
242 if (bp != &sblk)
243 return;
244 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
245 int size = sblock->fs_cssize - i < sblock->fs_bsize ?
246 sblock->fs_cssize - i : sblock->fs_bsize;
247 ccsp = (struct csum *)((char *)sblock->fs_csp + i);
248 if (needswap)
249 ffs_csum_swap(ccsp, ccsp, size);
250 bwrite(fswritefd, (char *)ccsp,
251 FFS_FSBTODB(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
252 size);
253 if (needswap)
254 ffs_csum_swap(ccsp, ccsp, size);
255 }
256 }
257
258 static void
259 rwerror(const char *mesg, daddr_t blk)
260 {
261
262 if (preen == 0)
263 printf("\n");
264 pfatal("CANNOT %s: BLK %lld", mesg, (long long)blk);
265 if (reply("CONTINUE") == 0)
266 exit(FSCK_EXIT_CHECK_FAILED);
267 }
268
269 void
270 ckfini(int noint)
271 {
272 struct bufarea *bp, *nbp;
273 int cnt = 0;
274
275 if (!noint) {
276 if (doinglevel2)
277 return;
278 markclean = 0;
279 }
280
281 if (fswritefd < 0) {
282 (void)close(fsreadfd);
283 return;
284 }
285 flush(fswritefd, &sblk);
286 if (havesb && bflag != 0 &&
287 (preen || reply("UPDATE STANDARD SUPERBLOCK"))) {
288 if (preen)
289 pwarn("UPDATING STANDARD SUPERBLOCK\n");
290 if (!is_ufs2 && (sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0)
291 sblk.b_bno = SBLOCK_UFS1 / dev_bsize;
292 else
293 sblk.b_bno = sblock->fs_sblockloc / dev_bsize;
294 sbdirty();
295 flush(fswritefd, &sblk);
296 }
297 #ifndef NO_APPLE_UFS
298 flush(fswritefd, &appleufsblk);
299 free(appleufsblk.b_un.b_buf);
300 #endif
301 flush(fswritefd, &cgblk);
302 free(cgblk.b_un.b_buf);
303 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
304 cnt++;
305 flush(fswritefd, bp);
306 nbp = bp->b_prev;
307 free(bp->b_un.b_buf);
308 free((char *)bp);
309 }
310 if (bufhead.b_size != cnt)
311 errexit("Panic: lost %d buffers", bufhead.b_size - cnt);
312 pbp = pdirbp = (struct bufarea *)0;
313 if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
314 /*
315 * Mark the file system as clean, and sync the superblock.
316 */
317 if (preen)
318 pwarn("MARKING FILE SYSTEM CLEAN\n");
319 else if (!reply("MARK FILE SYSTEM CLEAN"))
320 markclean = 0;
321 if (markclean) {
322 sblock->fs_clean = FS_ISCLEAN;
323 sblock->fs_pendingblocks = 0;
324 sblock->fs_pendinginodes = 0;
325 sbdirty();
326 flush(fswritefd, &sblk);
327 if (!preen)
328 printf(
329 "\n***** FILE SYSTEM MARKED CLEAN *****\n");
330 }
331 }
332 if (doing2ea) {
333 printf("ENABLING EXTATTR SUPPORT\n");
334 is_ufs2ea = 1;
335 sbdirty();
336 flush(fswritefd, &sblk);
337 }
338 if (doing2noea) {
339 printf("DISABLING EXTATTR SUPPORT\n");
340 is_ufs2ea = 0;
341 sbdirty();
342 flush(fswritefd, &sblk);
343 }
344 if (debug)
345 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
346 totalreads, (int)(diskreads * 100 / totalreads));
347 cleanup_wapbl();
348 (void)close(fsreadfd);
349 (void)close(fswritefd);
350 }
351
352 int
353 bread(int fd, char *buf, daddr_t blk, long size)
354 {
355 char *cp;
356 int i, errs;
357 off_t offset;
358
359 offset = blk;
360 offset *= dev_bsize;
361 if ((pread(fd, buf, (int)size, offset) == size) &&
362 read_wapbl(buf, size, blk) == 0)
363 return (0);
364 rwerror("READ", blk);
365 errs = 0;
366 memset(buf, 0, (size_t)size);
367 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
368 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
369 if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
370 if (secsize != dev_bsize && dev_bsize != 1)
371 printf(" %lld (%lld),",
372 (long long)((blk*dev_bsize + i) / secsize),
373 (long long)(blk + i / dev_bsize));
374 else
375 printf(" %lld,",
376 (long long)(blk + i / dev_bsize));
377 errs++;
378 }
379 }
380 printf("\n");
381 return (errs);
382 }
383
384 void
385 bwrite(int fd, char *buf, daddr_t blk, long size)
386 {
387 int i;
388 char *cp;
389 off_t offset;
390
391 if (fd < 0)
392 return;
393 offset = blk;
394 offset *= dev_bsize;
395 if (pwrite(fd, buf, (int)size, offset) == size) {
396 fsmodified = 1;
397 return;
398 }
399 rwerror("WRITE", blk);
400 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
401 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
402 if (pwrite(fd, cp, (int)dev_bsize, offset + i) != dev_bsize)
403 printf(" %lld,", (long long)(blk + i / dev_bsize));
404 printf("\n");
405 return;
406 }
407
408 /*
409 * allocate a data block with the specified number of fragments
410 */
411 daddr_t
412 allocblk(long frags)
413 {
414 int i, j, k, cg, baseblk;
415 struct cg *cgp = cgrp;
416
417 if (frags <= 0 || frags > sblock->fs_frag)
418 return (0);
419 for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
420 for (j = 0; j <= sblock->fs_frag - frags; j++) {
421 if (testbmap(i + j))
422 continue;
423 for (k = 1; k < frags; k++)
424 if (testbmap(i + j + k))
425 break;
426 if (k < frags) {
427 j += k;
428 continue;
429 }
430 cg = dtog(sblock, i + j);
431 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
432 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
433 if ((doswap && !needswap) || (!doswap && needswap))
434 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
435 if (!cg_chkmagic(cgp, 0))
436 pfatal("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n",
437 cg);
438 baseblk = dtogd(sblock, i + j);
439 for (k = 0; k < frags; k++) {
440 setbmap(i + j + k);
441 clrbit(cg_blksfree(cgp, 0), baseblk + k);
442 }
443 n_blks += frags;
444 if (frags == sblock->fs_frag) {
445 cgp->cg_cs.cs_nbfree--;
446 sblock->fs_cstotal.cs_nbfree--;
447 sblock->fs_cs(fs, cg).cs_nbfree--;
448 ffs_clusteracct(sblock, cgp,
449 ffs_fragstoblks(sblock, baseblk), -1);
450 } else {
451 cgp->cg_cs.cs_nffree -= frags;
452 sblock->fs_cstotal.cs_nffree -= frags;
453 sblock->fs_cs(fs, cg).cs_nffree -= frags;
454 }
455 sbdirty();
456 cgdirty();
457 return (i + j);
458 }
459 }
460 return (0);
461 }
462
463 /*
464 * Free a previously allocated block
465 */
466 void
467 freeblk(daddr_t blkno, long frags)
468 {
469 struct inodesc idesc;
470
471 memset(&idesc, 0, sizeof(idesc));
472 idesc.id_blkno = blkno;
473 idesc.id_numfrags = frags;
474 (void)pass4check(&idesc);
475 }
476
477 /*
478 * Find a pathname
479 */
480 void
481 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
482 {
483 int len;
484 char *cp;
485 struct inodesc idesc;
486 static int busy = 0;
487 struct inostat *info;
488
489 if (curdir == ino && ino == UFS_ROOTINO) {
490 (void)strlcpy(namebuf, "/", namebuflen);
491 return;
492 }
493 info = inoinfo(curdir);
494 if (busy || (info->ino_state != DSTATE && info->ino_state != DFOUND)) {
495 (void)strlcpy(namebuf, "?", namebuflen);
496 return;
497 }
498 busy = 1;
499 memset(&idesc, 0, sizeof(struct inodesc));
500 idesc.id_type = DATA;
501 idesc.id_fix = IGNORE;
502 cp = &namebuf[MAXPATHLEN - 1];
503 *cp = '\0';
504 if (curdir != ino) {
505 idesc.id_parent = curdir;
506 goto namelookup;
507 }
508 while (ino != UFS_ROOTINO) {
509 idesc.id_number = ino;
510 idesc.id_func = findino;
511 idesc.id_name = "..";
512 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
513 break;
514 namelookup:
515 idesc.id_number = idesc.id_parent;
516 idesc.id_parent = ino;
517 idesc.id_func = findname;
518 idesc.id_name = namebuf;
519 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
520 break;
521 len = strlen(namebuf);
522 cp -= len;
523 memmove(cp, namebuf, (size_t)len);
524 *--cp = '/';
525 if (cp < &namebuf[FFS_MAXNAMLEN])
526 break;
527 ino = idesc.id_number;
528 }
529 busy = 0;
530 if (ino != UFS_ROOTINO)
531 *--cp = '?';
532 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
533 }
534
535 /*
536 * determine whether an inode should be fixed.
537 */
538 int
539 dofix(struct inodesc *idesc, const char *msg)
540 {
541
542 switch (idesc->id_fix) {
543
544 case DONTKNOW:
545 if (idesc->id_type == DATA)
546 direrror(idesc->id_number, msg);
547 else
548 pwarn("%s", msg);
549 if (preen) {
550 printf(" (SALVAGED)\n");
551 idesc->id_fix = FIX;
552 return (ALTERED);
553 }
554 if (reply("SALVAGE") == 0) {
555 idesc->id_fix = NOFIX;
556 return (0);
557 }
558 idesc->id_fix = FIX;
559 return (ALTERED);
560
561 case FIX:
562 return (ALTERED);
563
564 case NOFIX:
565 case IGNORE:
566 return (0);
567
568 default:
569 errexit("UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
570 }
571 /* NOTREACHED */
572 return (0);
573 }
574
575 void
576 copyback_cg(struct bufarea *blk)
577 {
578
579 memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
580 if (needswap)
581 ffs_cg_swap(cgrp, blk->b_un.b_cg, sblock);
582 }
583
584 void
585 infohandler(int sig)
586 {
587 got_siginfo = 1;
588 }
589
590 /*
591 * Look up state information for an inode.
592 */
593 struct inostat *
594 inoinfo(ino_t inum)
595 {
596 static struct inostat unallocated = { USTATE, 0, 0 };
597 struct inostatlist *ilp;
598 size_t iloff;
599
600 if (inum > maxino)
601 errexit("inoinfo: inumber %llu out of range",
602 (unsigned long long)inum);
603 ilp = &inostathead[inum / sblock->fs_ipg];
604 iloff = inum % sblock->fs_ipg;
605 if (iloff >= ilp->il_numalloced)
606 return (&unallocated);
607 return (&ilp->il_stat[iloff]);
608 }
609
610 void
611 sb_oldfscompat_read(struct fs *fs, struct fs **fssave)
612 {
613 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
614 (fs->fs_old_flags & FS_FLAGS_UPDATED))
615 return;
616
617 /* Save a copy of fields that may be modified for compatibility */
618 if (fssave) {
619 if (!*fssave)
620 *fssave = malloc(sizeof(struct fs));
621 if (!*fssave)
622 errexit("cannot allocate space for compat store");
623 memmove(*fssave, fs, sizeof(struct fs));
624
625 if (debug)
626 printf("detected ufs1 superblock not yet updated for ufs2 kernels\n");
627
628 if (doswap) {
629 uint16_t postbl[256];
630 int i, n;
631
632 if (fs->fs_old_postblformat == FS_42POSTBLFMT)
633 n = 256;
634 else
635 n = 128;
636
637 /* extract the postbl from the unswapped superblock */
638 if (!needswap)
639 ffs_sb_swap(*fssave, *fssave);
640 memmove(postbl, (&(*fssave)->fs_old_postbl_start),
641 n * sizeof(postbl[0]));
642 if (!needswap)
643 ffs_sb_swap(*fssave, *fssave);
644
645 /* Now swap it */
646 for (i=0; i < n; i++)
647 postbl[i] = bswap16(postbl[i]);
648
649 /* And put it back such that it will get correctly
650 * unscrambled if it is swapped again on the way out
651 */
652 if (needswap)
653 ffs_sb_swap(*fssave, *fssave);
654 memmove((&(*fssave)->fs_old_postbl_start), postbl,
655 n * sizeof(postbl[0]));
656 if (needswap)
657 ffs_sb_swap(*fssave, *fssave);
658 }
659
660 }
661
662 /* These fields will be overwritten by their
663 * original values in fs_oldfscompat_write, so it is harmless
664 * to modify them here.
665 */
666 fs->fs_cstotal.cs_ndir =
667 fs->fs_old_cstotal.cs_ndir;
668 fs->fs_cstotal.cs_nbfree =
669 fs->fs_old_cstotal.cs_nbfree;
670 fs->fs_cstotal.cs_nifree =
671 fs->fs_old_cstotal.cs_nifree;
672 fs->fs_cstotal.cs_nffree =
673 fs->fs_old_cstotal.cs_nffree;
674
675 fs->fs_maxbsize = fs->fs_bsize;
676 fs->fs_time = fs->fs_old_time;
677 fs->fs_size = fs->fs_old_size;
678 fs->fs_dsize = fs->fs_old_dsize;
679 fs->fs_csaddr = fs->fs_old_csaddr;
680 fs->fs_sblockloc = SBLOCK_UFS1;
681
682 fs->fs_flags = fs->fs_old_flags;
683
684 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
685 fs->fs_old_nrpos = 8;
686 fs->fs_old_npsect = fs->fs_old_nsect;
687 fs->fs_old_interleave = 1;
688 fs->fs_old_trackskew = 0;
689 }
690 }
691
692 void
693 sb_oldfscompat_write(struct fs *fs, struct fs *fssave)
694 {
695 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
696 (fs->fs_old_flags & FS_FLAGS_UPDATED))
697 return;
698
699 fs->fs_old_flags = fs->fs_flags;
700 fs->fs_old_time = fs->fs_time;
701 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
702 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
703 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
704 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
705
706 fs->fs_flags = fssave->fs_flags;
707
708 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
709 fs->fs_old_nrpos = fssave->fs_old_nrpos;
710 fs->fs_old_npsect = fssave->fs_old_npsect;
711 fs->fs_old_interleave = fssave->fs_old_interleave;
712 fs->fs_old_trackskew = fssave->fs_old_trackskew;
713 }
714
715 memmove(&fs->fs_old_postbl_start, &fssave->fs_old_postbl_start,
716 ((fs->fs_old_postblformat == FS_42POSTBLFMT) ?
717 512 : 256));
718 }
719
720 struct uquot *
721 find_uquot(struct uquot_hash *uq_hash, uint32_t uid, int alloc)
722 {
723 struct uquot *uq;
724 SLIST_FOREACH(uq, &uq_hash[uid & q2h_hash_mask], uq_entries) {
725 if (uq->uq_uid == uid)
726 return uq;
727 }
728 if (!alloc)
729 return NULL;
730 uq = malloc(sizeof(struct uquot));
731 if (uq == NULL)
732 errexit("cannot allocate quota entry");
733 memset(uq, 0, sizeof(struct uquot));
734 uq->uq_uid = uid;
735 SLIST_INSERT_HEAD(&uq_hash[uid & q2h_hash_mask], uq, uq_entries);
736 return uq;
737 }
738
739 void
740 remove_uquot(struct uquot_hash *uq_hash, struct uquot *uq)
741 {
742 SLIST_REMOVE(&uq_hash[uq->uq_uid & q2h_hash_mask],
743 uq, uquot, uq_entries);
744 }
745
746 void
747 update_uquot(ino_t inum, uid_t uid, gid_t gid, int64_t bchange, int64_t ichange)
748 {
749 /* simple uquot cache: remember the last used */
750 static struct uquot *uq_u = NULL;
751 static struct uquot *uq_g = NULL;
752
753 if (inum < UFS_ROOTINO)
754 return;
755 if (is_journal_inode(inum))
756 return;
757 if (is_quota_inode(inum))
758 return;
759
760 if (uquot_user_hash == NULL)
761 return;
762
763 if (uq_u == NULL || uq_u->uq_uid != uid)
764 uq_u = find_uquot(uquot_user_hash, uid, 1);
765 uq_u->uq_b += bchange;
766 uq_u->uq_i += ichange;
767 if (uq_g == NULL || uq_g->uq_uid != gid)
768 uq_g = find_uquot(uquot_group_hash, gid, 1);
769 uq_g->uq_b += bchange;
770 uq_g->uq_i += ichange;
771 }
772
773 int
774 is_quota_inode(ino_t inum)
775 {
776
777 if ((sblock->fs_flags & FS_DOQUOTA2) == 0)
778 return 0;
779
780 if (sblock->fs_quota_magic != Q2_HEAD_MAGIC)
781 return 0;
782
783 if (sblock->fs_quotafile[USRQUOTA] == inum)
784 return 1;
785
786 if (sblock->fs_quotafile[GRPQUOTA] == inum)
787 return 1;
788
789 return 0;
790 }
791