xref: /openbsd-src/sbin/fsck_ffs/pass4.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: pass4.c,v 1.20 2011/04/16 16:37:21 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 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <ufs/ufs/dinode.h>
36 #include <ufs/ffs/fs.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "fsutil.h"
42 #include "fsck.h"
43 #include "extern.h"
44 
45 static ino_t info_inumber;
46 
47 static int
48 pass4_info(char *buf, size_t buflen)
49 {
50 	return (snprintf(buf, buflen, "phase 4, inode %d/%d",
51 	    info_inumber, lastino) > 0);
52 }
53 
54 void
55 pass4(void)
56 {
57 	ino_t inumber;
58 	struct zlncnt *zlnp;
59 	union dinode *dp;
60 	struct inodesc idesc;
61 	int n, c, i;
62 
63 	memset(&idesc, 0, sizeof(struct inodesc));
64 	idesc.id_type = ADDR;
65 	idesc.id_func = pass4check;
66 	info_fn = pass4_info;
67 	for (c = 0; c < sblock.fs_ncg; c++) {
68 		inumber = c * sblock.fs_ipg;
69 		for (i = 0; i < inostathead[c].il_numalloced; i++, inumber++) {
70 			if (inumber < ROOTINO)
71 				continue;
72  			idesc.id_number = inumber;
73 			switch (GET_ISTATE(inumber)) {
74 
75 			case FSTATE:
76 			case DFOUND:
77 				n = ILNCOUNT(inumber);
78 				if (n) {
79 					adjust(&idesc, (short)n);
80 					break;
81 				}
82 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
83 					if (zlnp->zlncnt == inumber) {
84 						zlnp->zlncnt = zlnhead->zlncnt;
85 						zlnp = zlnhead;
86 						zlnhead = zlnhead->next;
87 						free((char *)zlnp);
88 						clri(&idesc, "UNREF", 1);
89 						break;
90 					}
91 				break;
92 
93 			case DSTATE:
94 				clri(&idesc, "UNREF", 1);
95 				break;
96 
97 			case DCLEAR:
98 				dp = ginode(inumber);
99 				if (DIP(dp, di_size) == 0) {
100 					clri(&idesc, "ZERO LENGTH", 1);
101 					break;
102 				}
103 				/* FALLTHROUGH */
104 			case FCLEAR:
105 				clri(&idesc, "BAD/DUP", 1);
106 				break;
107 
108 			case USTATE:
109 				break;
110 
111 			default:
112 				errexit("BAD STATE %d FOR INODE I=%d\n",
113 			    	GET_ISTATE(inumber), inumber);
114 			}
115 		}
116 	}
117 	info_fn = NULL;
118 }
119 
120 int
121 pass4check(struct inodesc *idesc)
122 {
123 	struct dups *dlp;
124 	int nfrags, res = KEEPON;
125 	daddr64_t blkno = idesc->id_blkno;
126 
127 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
128 		if (chkrange(blkno, 1)) {
129 			res = SKIP;
130 		} else if (testbmap(blkno)) {
131 			for (dlp = duplist; dlp; dlp = dlp->next) {
132 				if (dlp->dup != blkno)
133 					continue;
134 				dlp->dup = duplist->dup;
135 				dlp = duplist;
136 				duplist = duplist->next;
137 				free(dlp);
138 				break;
139 			}
140 			if (dlp == 0) {
141 				clrbmap(blkno);
142 				n_blks--;
143 			}
144 		}
145 	}
146 	return (res);
147 }
148