xref: /openbsd/sbin/fsck_ffs/pass4.c (revision 133306f0)
1 /*	$OpenBSD: pass4.c,v 1.4 1999/03/01 07:45:18 d Exp $	*/
2 /*	$NetBSD: pass4.c,v 1.11 1996/09/27 22:45:17 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pass4.c	8.1 (Berkeley) 6/5/93";
40 #else
41 static char rcsid[] = "$OpenBSD: pass4.c,v 1.4 1999/03/01 07:45:18 d Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ffs/fs.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "fsutil.h"
54 #include "fsck.h"
55 #include "extern.h"
56 
57 static ino_t info_inumber;
58 
59 static int
60 pass4_info(buf, buflen)
61         char * buf;
62 	int buflen;
63 {
64 	return snprintf(buf, buflen, "phase 4, inode %d/%d",
65 		info_inumber, lastino);
66 }
67 
68 void
69 pass4()
70 {
71 	register ino_t inumber;
72 	register struct zlncnt *zlnp;
73 	struct dinode *dp;
74 	struct inodesc idesc;
75 	int n;
76 
77 	memset(&idesc, 0, sizeof(struct inodesc));
78 	idesc.id_type = ADDR;
79 	idesc.id_func = pass4check;
80 	info_fn = pass4_info;
81 	for (inumber = ROOTINO; inumber <= lastino; inumber++) {
82 		info_inumber = inumber;
83 		idesc.id_number = inumber;
84 		switch (statemap[inumber]) {
85 
86 		case FSTATE:
87 		case DFOUND:
88 			n = lncntp[inumber];
89 			if (n)
90 				adjust(&idesc, (short)n);
91 			else {
92 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
93 					if (zlnp->zlncnt == inumber) {
94 						zlnp->zlncnt = zlnhead->zlncnt;
95 						zlnp = zlnhead;
96 						zlnhead = zlnhead->next;
97 						free((char *)zlnp);
98 						clri(&idesc, "UNREF", 1);
99 						break;
100 					}
101 			}
102 			break;
103 
104 		case DSTATE:
105 			clri(&idesc, "UNREF", 1);
106 			break;
107 
108 		case DCLEAR:
109 			dp = ginode(inumber);
110 			if (dp->di_size == 0) {
111 				clri(&idesc, "ZERO LENGTH", 1);
112 				break;
113 			}
114 			/* fall through */
115 		case FCLEAR:
116 			clri(&idesc, "BAD/DUP", 1);
117 			break;
118 
119 		case USTATE:
120 			break;
121 
122 		default:
123 			errexit("BAD STATE %d FOR INODE I=%d",
124 			    statemap[inumber], inumber);
125 		}
126 	}
127 	info_fn = NULL;
128 }
129 
130 int
131 pass4check(idesc)
132 	register struct inodesc *idesc;
133 {
134 	register struct dups *dlp;
135 	int nfrags, res = KEEPON;
136 	daddr_t blkno = idesc->id_blkno;
137 
138 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
139 		if (chkrange(blkno, 1)) {
140 			res = SKIP;
141 		} else if (testbmap(blkno)) {
142 			for (dlp = duplist; dlp; dlp = dlp->next) {
143 				if (dlp->dup != blkno)
144 					continue;
145 				dlp->dup = duplist->dup;
146 				dlp = duplist;
147 				duplist = duplist->next;
148 				free((char *)dlp);
149 				break;
150 			}
151 			if (dlp == 0) {
152 				clrbmap(blkno);
153 				n_blks--;
154 			}
155 		}
156 	}
157 	return (res);
158 }
159