xref: /openbsd-src/sbin/fsck_ffs/pass4.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: pass4.c,v 1.18 2008/06/10 23:10:29 otto 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.18 2008/06/10 23:10:29 otto 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
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
63 pass4(void)
64 {
65 	ino_t inumber;
66 	struct zlncnt *zlnp;
67 	union dinode *dp;
68 	struct inodesc idesc;
69 	int n, c, i;
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 (c = 0; c < sblock.fs_ncg; c++) {
76 		inumber = c * sblock.fs_ipg;
77 		for (i = 0; i < cginosused[c]; i++, inumber++) {
78 			if (inumber < ROOTINO)
79 				continue;
80  			idesc.id_number = inumber;
81 			switch (GET_ISTATE(inumber)) {
82 
83 			case FSTATE:
84 			case DFOUND:
85 				n = lncntp[inumber];
86 				if (n) {
87 					adjust(&idesc, (short)n);
88 					break;
89 				}
90 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
91 					if (zlnp->zlncnt == inumber) {
92 						zlnp->zlncnt = zlnhead->zlncnt;
93 						zlnp = zlnhead;
94 						zlnhead = zlnhead->next;
95 						free((char *)zlnp);
96 						clri(&idesc, "UNREF", 1);
97 						break;
98 					}
99 				break;
100 
101 			case DSTATE:
102 				clri(&idesc, "UNREF", 1);
103 				break;
104 
105 			case DCLEAR:
106 				dp = ginode(inumber);
107 				if (DIP(dp, di_size) == 0) {
108 					clri(&idesc, "ZERO LENGTH", 1);
109 					break;
110 				}
111 				/* FALLTHROUGH */
112 			case FCLEAR:
113 				clri(&idesc, "BAD/DUP", 1);
114 				break;
115 
116 			case USTATE:
117 				break;
118 
119 			default:
120 				errexit("BAD STATE %d FOR INODE I=%d\n",
121 			    	GET_ISTATE(inumber), inumber);
122 			}
123 		}
124 	}
125 	info_fn = NULL;
126 }
127 
128 int
129 pass4check(struct inodesc *idesc)
130 {
131 	struct dups *dlp;
132 	int nfrags, res = KEEPON;
133 	daddr64_t blkno = idesc->id_blkno;
134 
135 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
136 		if (chkrange(blkno, 1)) {
137 			res = SKIP;
138 		} else if (testbmap(blkno)) {
139 			for (dlp = duplist; dlp; dlp = dlp->next) {
140 				if (dlp->dup != blkno)
141 					continue;
142 				dlp->dup = duplist->dup;
143 				dlp = duplist;
144 				duplist = duplist->next;
145 				free(dlp);
146 				break;
147 			}
148 			if (dlp == 0) {
149 				clrbmap(blkno);
150 				n_blks--;
151 			}
152 		}
153 	}
154 	return (res);
155 }
156