Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / fsck_lfs / pass4.c
blobe859db36e9940ea648d0bd49969875a5353328ba
1 /* $NetBSD: pass4.c,v 1.15 2006/09/01 19:52:48 perseant Exp $ */
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/mount.h>
35 #include <ufs/ufs/inode.h>
37 #define vnode uvnode
38 #define buf ubuf
39 #define panic call_panic
40 #include <ufs/lfs/lfs.h>
42 #include <err.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include "bufcache.h"
47 #include "vnode.h"
48 #include "lfs_user.h"
50 #include "fsutil.h"
51 #include "fsck.h"
52 #include "extern.h"
54 extern SEGUSE *seg_table;
56 static int check_orphan(struct inodesc *idp);
58 static int
59 check_orphan(struct inodesc *idp)
61 struct zlncnt *zlnp;
62 ino_t inumber = idp->id_number;
64 for (zlnp = orphead; zlnp; zlnp = zlnp->next) {
65 if (zlnp->zlncnt == inumber) {
66 /* Swap this with head */
67 zlnp->zlncnt = orphead->zlncnt;
68 zlnp = orphead;
69 orphead = orphead->next;
70 /* Free old head */
71 free((char *) zlnp);
72 clri(idp, "PROPERLY ORPHANED", 1);
73 return 1;
76 return 0;
79 void
80 pass4(void)
82 ino_t inumber;
83 struct zlncnt *zlnp;
84 struct ufs1_dinode *dp;
85 struct inodesc idesc;
86 int n;
88 memset(&idesc, 0, sizeof(struct inodesc));
89 idesc.id_type = ADDR;
90 idesc.id_func = pass4check;
91 for (inumber = ROOTINO; inumber <= lastino; inumber++) {
92 idesc.id_number = inumber;
93 switch (statemap[inumber]) {
95 case FSTATE:
96 case DFOUND:
97 n = lncntp[inumber];
98 if (n)
99 adjust(&idesc, (short) n);
100 else {
101 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
102 if (zlnp->zlncnt == inumber) {
103 zlnp->zlncnt = zlnhead->zlncnt;
104 zlnp = zlnhead;
105 zlnhead = zlnhead->next;
106 free((char *) zlnp);
107 clri(&idesc, "UNREF", 1);
108 break;
111 break;
113 case DSTATE:
114 clri(&idesc, "UNREF", 1);
115 break;
117 case DCLEAR:
118 if (check_orphan(&idesc))
119 break;
120 dp = ginode(inumber);
121 if (dp->di_size == 0) {
122 const char * msg = (lncntp[inumber] ?
123 "ZERO LENGTH" : "UNREF ZERO LENGTH");
124 clri(&idesc, msg, 1);
125 break;
127 clri(&idesc, "BAD/DUP", 1);
128 break;
130 case FCLEAR:
131 if (check_orphan(&idesc))
132 break;
133 clri(&idesc, "BAD/DUP", 1);
134 break;
136 case USTATE:
137 break;
139 default:
140 err(EEXIT, "BAD STATE %d FOR INODE I=%llu\n",
141 statemap[inumber], (unsigned long long)inumber);
147 pass4check(struct inodesc * idesc)
149 struct dups *dlp;
150 int ndblks, res = KEEPON;
151 daddr_t blkno = idesc->id_blkno;
152 SEGUSE *sup;
153 struct ubuf *bp;
154 int sn;
156 sn = dtosn(fs, blkno);
157 for (ndblks = fragstofsb(fs, idesc->id_numfrags); ndblks > 0; blkno++, ndblks--) {
158 if (chkrange(blkno, 1)) {
159 res = SKIP;
160 } else if (testbmap(blkno) || preen) {
161 for (dlp = duplist; dlp; dlp = dlp->next) {
162 if (dlp->dup != blkno)
163 continue;
164 dlp->dup = duplist->dup;
165 dlp = duplist;
166 duplist = duplist->next;
167 free((char *) dlp);
168 break;
170 if (dlp == 0) {
171 clrbmap(blkno);
172 LFS_SEGENTRY(sup, fs, sn, bp);
173 sup->su_nbytes -= fsbtob(fs, 1);
174 VOP_BWRITE(bp);
175 seg_table[sn].su_nbytes -= fsbtob(fs, 1);
176 ++fs->lfs_bfree;
177 n_blks--;
181 return (res);