xref: /freebsd/sbin/fsck_ffs/pass4.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #endif
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <err.h>
42 #include <stdint.h>
43 #include <string.h>
44 
45 #include "fsck.h"
46 
47 void
48 pass4(void)
49 {
50 	ino_t inumber;
51 	struct inode ip;
52 	struct inodesc idesc;
53 	int i, n, cg;
54 
55 	memset(&idesc, 0, sizeof(struct inodesc));
56 	idesc.id_func = freeblock;
57 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
58 		if (got_siginfo) {
59 			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
60 			    cdevname, cg, sblock.fs_ncg,
61 			    cg * 100 / sblock.fs_ncg);
62 			got_siginfo = 0;
63 		}
64 		if (got_sigalarm) {
65 			setproctitle("%s p4 %d%%", cdevname,
66 			    cg * 100 / sblock.fs_ncg);
67 			got_sigalarm = 0;
68 		}
69 		inumber = cg * sblock.fs_ipg;
70 		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
71 			if (inumber < UFS_ROOTINO)
72 				continue;
73 			idesc.id_number = inumber;
74 			idesc.id_type = inoinfo(inumber)->ino_idtype;
75 			switch (inoinfo(inumber)->ino_state) {
76 
77 			case FZLINK:
78 			case DZLINK:
79 				if (inoinfo(inumber)->ino_linkcnt == 0) {
80 					clri(&idesc, "UNREF", 1);
81 					break;
82 				}
83 				/* fall through */
84 
85 			case FSTATE:
86 			case DFOUND:
87 				n = inoinfo(inumber)->ino_linkcnt;
88 				if (n) {
89 					adjust(&idesc, (short)n);
90 					break;
91 				}
92 				break;
93 
94 			case DSTATE:
95 				clri(&idesc, "UNREF", 1);
96 				break;
97 
98 			case DCLEAR:
99 				/* if on snapshot, already cleared */
100 				if (cursnapshot != 0)
101 					break;
102 				ginode(inumber, &ip);
103 				if (DIP(ip.i_dp, di_size) == 0) {
104 					clri(&idesc, "ZERO LENGTH", 1);
105 					irelse(&ip);
106 					break;
107 				}
108 				irelse(&ip);
109 				/* fall through */
110 			case FCLEAR:
111 				clri(&idesc, "BAD/DUP", 1);
112 				break;
113 
114 			case USTATE:
115 				break;
116 
117 			default:
118 				errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
119 				    inoinfo(inumber)->ino_state,
120 				    (uintmax_t)inumber);
121 			}
122 		}
123 	}
124 }
125