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