xref: /netbsd-src/sbin/fsck_lfs/pass4.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: pass4.c,v 1.28 2020/04/03 19:36:33 joerg Exp $	 */
2 
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 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/mount.h>
35 
36 #define vnode uvnode
37 #define buf ubuf
38 #define panic call_panic
39 #include <ufs/lfs/lfs.h>
40 #include <ufs/lfs/lfs_accessors.h>
41 #include <ufs/lfs/lfs_inode.h>
42 
43 #include <err.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "bufcache.h"
48 #include "lfs_user.h"
49 
50 #include "fsutil.h"
51 #include "fsck.h"
52 #include "extern.h"
53 
54 static int check_orphan(struct inodesc *idp);
55 
56 static int
57 check_orphan(struct inodesc *idp)
58 {
59 	struct zlncnt *zlnp;
60 	ino_t inumber = idp->id_number;
61 
62 	for (zlnp = orphead; zlnp; zlnp = zlnp->next) {
63 		if (zlnp->zlncnt == inumber) {
64 			/* Swap this with head */
65 			zlnp->zlncnt = orphead->zlncnt;
66 			zlnp = orphead;
67 			orphead = orphead->next;
68 			/* Free old head */
69 			free((char *) zlnp);
70 			clri(idp, "PROPERLY ORPHANED", 1);
71 			return 1;
72 		}
73 	}
74 	return 0;
75 }
76 
77 void
78 pass4(void)
79 {
80 	ino_t inumber;
81 	struct zlncnt *zlnp;
82 	union lfs_dinode *dp;
83 	struct inodesc idesc;
84 	int n;
85 
86 	memset(&idesc, 0, sizeof(struct inodesc));
87 	idesc.id_type = ADDR;
88 	idesc.id_func = pass4check;
89 	for (inumber = ULFS_ROOTINO; inumber <= lastino; inumber++) {
90 		idesc.id_number = inumber;
91 		switch (statemap[inumber]) {
92 
93 		case FSTATE:
94 		case DFOUND:
95 			n = lncntp[inumber];
96 			if (n)
97 				adjust(&idesc, (short) n);
98 			else {
99 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
100 					if (zlnp->zlncnt == inumber) {
101 						zlnp->zlncnt = zlnhead->zlncnt;
102 						zlnp = zlnhead;
103 						zlnhead = zlnhead->next;
104 						free((char *) zlnp);
105 						clri(&idesc, "UNREF", 1);
106 						break;
107 					}
108 			}
109 			break;
110 
111 		case DSTATE:
112 			clri(&idesc, "UNREF", 1);
113 			break;
114 
115 		case DCLEAR:
116 			if (check_orphan(&idesc))
117 				break;
118 			dp = ginode(inumber);
119 			if (lfs_dino_getsize(fs, dp) == 0) {
120 				const char * msg = (lncntp[inumber] ?
121 					"ZERO LENGTH" : "UNREF ZERO LENGTH");
122 				clri(&idesc, msg, 1);
123 				break;
124 			}
125 			clri(&idesc, "BAD/DUP", 1);
126 			break;
127 
128 		case FCLEAR:
129 			if (check_orphan(&idesc))
130 				break;
131 			clri(&idesc, "BAD/DUP", 1);
132 			break;
133 
134 		case USTATE:
135 			break;
136 
137 		default:
138 			err(EEXIT, "BAD STATE %d FOR INODE I=%llu",
139 			    statemap[inumber], (unsigned long long)inumber);
140 		}
141 	}
142 }
143 
144 int
145 pass4check(struct inodesc * idesc)
146 {
147 	struct dups *dlp;
148 	int ndblks, res = KEEPON;
149 	daddr_t blkno = idesc->id_blkno;
150 	SEGUSE *sup;
151 	struct ubuf *bp;
152 	int sn;
153 
154 	sn = lfs_dtosn(fs, blkno);
155 	for (ndblks = idesc->id_numfrags; ndblks > 0; blkno++, ndblks--) {
156 		if (chkrange(blkno, 1)) {
157 			res = SKIP;
158 		} else if (testbmap(blkno) || preen) {
159 			for (dlp = duplist; dlp; dlp = dlp->next) {
160 				if (dlp->dup != blkno)
161 					continue;
162 				dlp->dup = duplist->dup;
163 				dlp = duplist;
164 				duplist = duplist->next;
165 				free((char *) dlp);
166 				break;
167 			}
168 			if (dlp == 0) {
169 				clrbmap(blkno);
170 				LFS_SEGENTRY(sup, fs, sn, bp);
171 				sup->su_nbytes -= lfs_fsbtob(fs, 1);
172 				VOP_BWRITE(bp);
173 				seg_table[sn].su_nbytes -= lfs_fsbtob(fs, 1);
174 				lfs_sb_addbfree(fs, 1);
175 				n_blks--;
176 			}
177 		}
178 	}
179 	return (res);
180 }
181