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