xref: /netbsd-src/sbin/fsck_lfs/pass4.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: pass4.c,v 1.8 2003/04/02 10:39:28 fvdl 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/mount.h>
39 #include <ufs/ufs/inode.h>
40 
41 #define vnode uvnode
42 #define buf ubuf
43 #define panic call_panic
44 #include <ufs/lfs/lfs.h>
45 
46 #include <err.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "bufcache.h"
51 #include "vnode.h"
52 #include "lfs.h"
53 
54 #include "fsutil.h"
55 #include "fsck.h"
56 #include "extern.h"
57 
58 extern SEGUSE *seg_table;
59 
60 void
61 pass4()
62 {
63 	register ino_t inumber;
64 	register struct zlncnt *zlnp;
65 	struct ufs1_dinode *dp;
66 	struct inodesc idesc;
67 	int n;
68 
69 	memset(&idesc, 0, sizeof(struct inodesc));
70 	idesc.id_type = ADDR;
71 	idesc.id_func = pass4check;
72 	for (inumber = ROOTINO; inumber <= lastino; inumber++) {
73 		idesc.id_number = inumber;
74 		switch (statemap[inumber]) {
75 
76 		case FSTATE:
77 		case DFOUND:
78 			n = lncntp[inumber];
79 			if (n)
80 				adjust(&idesc, (short) n);
81 			else {
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 			}
92 			break;
93 
94 		case DSTATE:
95 			clri(&idesc, "UNREF", 1);
96 			break;
97 
98 		case DCLEAR:
99 			dp = ginode(inumber);
100 			if (dp->di_size == 0) {
101 				clri(&idesc, "ZERO LENGTH", 1);
102 				break;
103 			}
104 			/* fall through */
105 		case FCLEAR:
106 			clri(&idesc, "BAD/DUP", 1);
107 			break;
108 
109 		case USTATE:
110 			break;
111 
112 		default:
113 			err(8, "BAD STATE %d FOR INODE I=%d\n",
114 			    statemap[inumber], inumber);
115 		}
116 	}
117 }
118 
119 int
120 pass4check(struct inodesc * idesc)
121 {
122 	register struct dups *dlp;
123 	int ndblks, res = KEEPON;
124 	daddr_t blkno = idesc->id_blkno;
125 	SEGUSE *sup;
126 	struct ubuf *bp;
127 	int sn, doanyway = 0;
128 
129 	sn = dtosn(fs, blkno);
130 	/* If preening, bmap is not valid for non-active segs */
131 	if (preen && !(seg_table[sn].su_flags & SEGUSE_ACTIVE))
132 		doanyway = 1;
133 	for (ndblks = fragstofsb(fs, idesc->id_numfrags); ndblks > 0; blkno++, ndblks--) {
134 		if (chkrange(blkno, 1)) {
135 			res = SKIP;
136 		} else if (testbmap(blkno) || doanyway) {
137 			for (dlp = duplist; dlp; dlp = dlp->next) {
138 				if (dlp->dup != blkno)
139 					continue;
140 				dlp->dup = duplist->dup;
141 				dlp = duplist;
142 				duplist = duplist->next;
143 				free((char *) dlp);
144 				break;
145 			}
146 			if (dlp == 0) {
147 				clrbmap(blkno);
148 				LFS_SEGENTRY(sup, fs, sn, bp);
149 				sup->su_nbytes -= fsbtob(fs, 1);
150 				VOP_BWRITE(bp);
151 				seg_table[sn].su_nbytes -= fsbtob(fs, 1);
152 				++fs->lfs_bfree;
153 				n_blks--;
154 			}
155 		}
156 	}
157 	return (res);
158 }
159