Home | History | Annotate | Line # | Download | only in fsck_ffs
      1 /*	$NetBSD: pass4.c,v 1.31 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[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
     36 #else
     37 __RCSID("$NetBSD: pass4.c,v 1.31 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 #include <sys/stat.h>
     44 
     45 #include <ufs/ufs/ufs_bswap.h>
     46 #include <ufs/ufs/dinode.h>
     47 #include <ufs/ffs/fs.h>
     48 #include <ufs/ffs/ffs_extern.h>
     49 
     50 #include <err.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 
     54 #include "fsutil.h"
     55 #include "fsck.h"
     56 #include "extern.h"
     57 
     58 void
     59 pass4(void)
     60 {
     61 	ino_t inumber;
     62 	struct zlncnt *zlnp;
     63 	union dinode *dp;
     64 	struct inodesc idesc;
     65 	int n;
     66 	size_t i;
     67 	uint32_t cg;
     68 	struct inostat *info;
     69 
     70 	memset(&idesc, 0, sizeof(struct inodesc));
     71 	idesc.id_func = pass4check;
     72 
     73 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
     74 		if (got_siginfo) {
     75 			fprintf(stderr,
     76 			    "%s: phase 4: cyl group %d of %d (%d%%)\n",
     77 			    cdevname(), cg, sblock->fs_ncg,
     78 			    cg * 100 / sblock->fs_ncg);
     79 			got_siginfo = 0;
     80 		}
     81 #ifdef PROGRESS
     82 		progress_bar(cdevname(), preen ? NULL : "phase 4",
     83 			    cg, sblock->fs_ncg);
     84 #endif /* PROGRESS */
     85 		inumber = cg * sblock->fs_ipg;
     86 		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
     87 			if (inumber < UFS_ROOTINO)
     88 				continue;
     89 			dp = ginode(inumber);
     90 			info = inoinfo(inumber);
     91 			idesc.id_number = inumber;
     92 			idesc.id_uid = iswap32(DIP(dp, uid));
     93 			idesc.id_gid = iswap32(DIP(dp, gid));
     94 			if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT)
     95 				idesc.id_type = SNAP;
     96 			else
     97 				idesc.id_type = ADDR;
     98 			switch (info->ino_state) {
     99 			case FSTATE:
    100 			case DFOUND:
    101 				n = info->ino_linkcnt;
    102 				if (n) {
    103 					if (is_journal_inode(inumber)) {
    104 						if (debug)
    105 							printf(
    106     "skipping unreferenced journal inode %" PRId64 "\n", inumber);
    107 						break;
    108 					} else if (is_quota_inode(inumber)) {
    109 						if (debug)
    110 							printf(
    111     "skipping unreferenced quota inode %" PRId64 "\n", inumber);
    112 						break;
    113 					} else {
    114 						adjust(&idesc, (short)n);
    115 					}
    116 					break;
    117 				}
    118 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    119 					if (zlnp->zlncnt == inumber) {
    120 						zlnp->zlncnt = zlnhead->zlncnt;
    121 						zlnp = zlnhead;
    122 						zlnhead = zlnhead->next;
    123 						free((char *)zlnp);
    124 						clri(&idesc, "UNREF", 1);
    125 						break;
    126 					}
    127 				break;
    128 
    129 			case DSTATE:
    130 				clri(&idesc, "UNREF", 1);
    131 				break;
    132 
    133 			case DCLEAR:
    134 				if (DIP(dp, size) == 0) {
    135 					clri(&idesc, "ZERO LENGTH", 1);
    136 					break;
    137 				}
    138 				/* fall through */
    139 			case FCLEAR:
    140 				clri(&idesc, "BAD/DUP", 1);
    141 				break;
    142 
    143 			case USTATE:
    144 				break;
    145 
    146 			default:
    147 				errexit("BAD STATE %d FOR INODE I=%llu",
    148 				    info->ino_state,
    149 				    (unsigned long long)inumber);
    150 			}
    151 		}
    152 	}
    153 #ifdef PROGRESS
    154 	if (!preen)
    155 		progress_done();
    156 #endif /* PROGRESS */
    157 }
    158 
    159 int
    160 pass4check(struct inodesc *idesc)
    161 {
    162 	struct dups *dlp;
    163 	int cg, nfrags, res = KEEPON;
    164 	daddr_t blkno = idesc->id_blkno;
    165 	struct cg *cgp = cgrp;
    166 
    167 	cg = dtog(sblock, blkno);
    168 	getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    169 	memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    170 	if ((doswap && !needswap) || (!doswap && needswap))
    171 		ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    172 	if (!cg_chkmagic(cgp, 0)) {
    173 		pwarn("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n", cg);
    174 		cgp = NULL;
    175 	}
    176 
    177 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    178 		if (chkrange(blkno, 1)) {
    179 			res = SKIP;
    180 		} else if (testbmap(blkno)) {
    181 			for (dlp = duplist; dlp; dlp = dlp->next) {
    182 				if (dlp->dup != blkno)
    183 					continue;
    184 				dlp->dup = duplist->dup;
    185 				dlp = duplist;
    186 				duplist = duplist->next;
    187 				free((char *)dlp);
    188 				break;
    189 			}
    190 			if (dlp == 0) {
    191 				clrbmap(blkno);
    192 				if (cgp)
    193 					setbit(cg_blksfree(cgp, 0),
    194 					    dtogd(sblock, blkno));
    195 
    196 				n_blks--;
    197 				if (idesc->id_type != SNAP) {
    198 					update_uquot(idesc->id_number,
    199 					    idesc->id_uid, idesc->id_gid,
    200 					    -btodb(sblock->fs_fsize), 0);
    201 				}
    202 				if (idesc->id_numfrags != sblock->fs_frag &&
    203 				    cgp) {
    204 					cgp->cg_cs.cs_nffree ++;
    205 					sblock->fs_cstotal.cs_nffree ++;
    206 					sblock->fs_cs(fs, cg).cs_nffree ++;
    207 				}
    208 			}
    209 		}
    210 	}
    211 	if (cgp && res != SKIP && idesc->id_numfrags == sblock->fs_frag) {
    212 		cgp->cg_cs.cs_nbfree++;
    213 		sblock->fs_cstotal.cs_nbfree++;
    214 		sblock->fs_cs(fs, cg).cs_nbfree++;
    215 		ffs_clusteracct(sblock, cgp,
    216 		    ffs_fragstoblks(sblock, dtogd(sblock, idesc->id_blkno)), 1);
    217 	}
    218 	sbdirty();
    219 	cgdirty();
    220 	return (res);
    221 }
    222