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