xref: /netbsd-src/sbin/fsck_ffs/pass4.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: pass4.c,v 1.28 2013/06/23 22:03:34 dholland 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.28 2013/06/23 22:03:34 dholland 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, i, cg;
66 	struct inostat *info;
67 
68 	memset(&idesc, 0, sizeof(struct inodesc));
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 < UFS_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 			if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT)
93 				idesc.id_type = SNAP;
94 			else
95 				idesc.id_type = ADDR;
96 			switch (info->ino_state) {
97 			case FSTATE:
98 			case DFOUND:
99 				n = info->ino_linkcnt;
100 				if (n) {
101 					if (is_journal_inode(inumber)) {
102 						if (debug)
103 							printf(
104     "skipping unreferenced journal inode %" PRId64 "\n", inumber);
105 						break;
106 					} else if (is_quota_inode(inumber)) {
107 						if (debug)
108 							printf(
109     "skipping unreferenced quota inode %" PRId64 "\n", inumber);
110 						break;
111 					} else {
112 						adjust(&idesc, (short)n);
113 					}
114 					break;
115 				}
116 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
117 					if (zlnp->zlncnt == inumber) {
118 						zlnp->zlncnt = zlnhead->zlncnt;
119 						zlnp = zlnhead;
120 						zlnhead = zlnhead->next;
121 						free((char *)zlnp);
122 						clri(&idesc, "UNREF", 1);
123 						break;
124 					}
125 				break;
126 
127 			case DSTATE:
128 				clri(&idesc, "UNREF", 1);
129 				break;
130 
131 			case DCLEAR:
132 				if (DIP(dp, size) == 0) {
133 					clri(&idesc, "ZERO LENGTH", 1);
134 					break;
135 				}
136 				/* fall through */
137 			case FCLEAR:
138 				clri(&idesc, "BAD/DUP", 1);
139 				break;
140 
141 			case USTATE:
142 				break;
143 
144 			default:
145 				errexit("BAD STATE %d FOR INODE I=%llu",
146 				    info->ino_state,
147 				    (unsigned long long)inumber);
148 			}
149 		}
150 	}
151 #ifdef PROGRESS
152 	if (!preen)
153 		progress_done();
154 #endif /* PROGRESS */
155 }
156 
157 int
158 pass4check(struct inodesc *idesc)
159 {
160 	struct dups *dlp;
161 	int cg, nfrags, res = KEEPON;
162 	daddr_t blkno = idesc->id_blkno;
163 	struct cg *cgp = cgrp;
164 
165 	cg = dtog(sblock, blkno);
166 	getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
167 	memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
168 	if ((doswap && !needswap) || (!doswap && needswap))
169 		ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
170 	if (!cg_chkmagic(cgp, 0)) {
171 		pwarn("CG %d: ALLOCBLK: BAD MAGIC NUMBER\n", cg);
172 		cgp = NULL;
173 	}
174 
175 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
176 		if (chkrange(blkno, 1)) {
177 			res = SKIP;
178 		} else if (testbmap(blkno)) {
179 			for (dlp = duplist; dlp; dlp = dlp->next) {
180 				if (dlp->dup != blkno)
181 					continue;
182 				dlp->dup = duplist->dup;
183 				dlp = duplist;
184 				duplist = duplist->next;
185 				free((char *)dlp);
186 				break;
187 			}
188 			if (dlp == 0) {
189 				clrbmap(blkno);
190 				if (cgp)
191 					setbit(cg_blksfree(cgp, 0),
192 					    dtogd(sblock, blkno));
193 
194 				n_blks--;
195 				if (idesc->id_type != SNAP) {
196 					update_uquot(idesc->id_number,
197 					    idesc->id_uid, idesc->id_gid,
198 					    -btodb(sblock->fs_fsize), 0);
199 				}
200 				if (idesc->id_numfrags != sblock->fs_frag &&
201 				    cgp) {
202 					cgp->cg_cs.cs_nffree ++;
203 					sblock->fs_cstotal.cs_nffree ++;
204 					sblock->fs_cs(fs, cg).cs_nffree ++;
205 				}
206 			}
207 		}
208 	}
209 	if (cgp && res != SKIP && idesc->id_numfrags == sblock->fs_frag) {
210 		cgp->cg_cs.cs_nbfree++;
211 		sblock->fs_cstotal.cs_nbfree++;
212 		sblock->fs_cs(fs, cg).cs_nbfree++;
213 		ffs_clusteracct(sblock, cgp,
214 		    ffs_fragstoblks(sblock, dtogd(sblock, idesc->id_blkno)), 1);
215 	}
216 	sbdirty();
217 	cgdirty();
218 	return (res);
219 }
220