1 /*	$OpenBSD: pass4.c,v 1.15 2006/03/22 20:24:32 deraadt 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pass4.c	8.1 (Berkeley) 6/5/93";
36 #else
37 static const char rcsid[] = "$OpenBSD: pass4.c,v 1.15 2006/03/22 20:24:32 deraadt Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "fsutil.h"
50 #include "fsck.h"
51 #include "extern.h"
52 
53 static ino_t info_inumber;
54 
55 static int
pass4_info(char * buf,size_t buflen)56 pass4_info(char *buf, size_t buflen)
57 {
58 	return (snprintf(buf, buflen, "phase 4, inode %d/%d",
59 	    info_inumber, lastino) > 0);
60 }
61 
62 void
pass4(void)63 pass4(void)
64 {
65 	ino_t inumber;
66 	struct zlncnt *zlnp;
67 	struct ufs1_dinode *dp;
68 	struct inodesc idesc;
69 	int n;
70 
71 	memset(&idesc, 0, sizeof(struct inodesc));
72 	idesc.id_type = ADDR;
73 	idesc.id_func = pass4check;
74 	info_fn = pass4_info;
75 	for (inumber = ROOTINO; inumber <= lastino; inumber++) {
76 		info_inumber = inumber;
77 		idesc.id_number = inumber;
78 		switch (statemap[inumber]) {
79 
80 		case FSTATE:
81 		case DFOUND:
82 			n = lncntp[inumber];
83 			if (n)
84 				adjust(&idesc, (short)n);
85 			else {
86 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
87 					if (zlnp->zlncnt == inumber) {
88 						zlnp->zlncnt = zlnhead->zlncnt;
89 						zlnp = zlnhead;
90 						zlnhead = zlnhead->next;
91 						free((char *)zlnp);
92 						clri(&idesc, "UNREF", 1);
93 						break;
94 					}
95 			}
96 			break;
97 
98 		case DSTATE:
99 			clri(&idesc, "UNREF", 1);
100 			break;
101 
102 		case DCLEAR:
103 			dp = ginode(inumber);
104 			if (dp->di_size == 0) {
105 				clri(&idesc, "ZERO LENGTH", 1);
106 				break;
107 			}
108 			/* FALLTHROUGH */
109 		case FCLEAR:
110 			clri(&idesc, "BAD/DUP", 1);
111 			break;
112 
113 		case USTATE:
114 			break;
115 
116 		default:
117 			errexit("BAD STATE %d FOR INODE I=%d\n",
118 			    statemap[inumber], inumber);
119 		}
120 	}
121 	info_fn = NULL;
122 }
123 
124 int
pass4check(struct inodesc * idesc)125 pass4check(struct inodesc *idesc)
126 {
127 	struct dups *dlp;
128 	int nfrags, res = KEEPON;
129 	daddr_t blkno = idesc->id_blkno;
130 
131 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
132 		if (chkrange(blkno, 1)) {
133 			res = SKIP;
134 		} else if (testbmap(blkno)) {
135 			for (dlp = duplist; dlp; dlp = dlp->next) {
136 				if (dlp->dup != blkno)
137 					continue;
138 				dlp->dup = duplist->dup;
139 				dlp = duplist;
140 				duplist = duplist->next;
141 				free(dlp);
142 				break;
143 			}
144 			if (dlp == 0) {
145 				clrbmap(blkno);
146 				n_blks--;
147 			}
148 		}
149 	}
150 	return (res);
151 }
152