1/* $NetBSD: pass4.c,v 1.16 2006/11/09 19:36:36 christos 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#include <ufs/ufs/inode.h>
36
37#define vnode uvnode
38#define buf ubuf
39#define panic call_panic
40#include <ufs/lfs/lfs.h>
41
42#include <err.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include "bufcache.h"
47#include "vnode.h"
48#include "lfs_user.h"
49
50#include "fsutil.h"
51#include "fsck.h"
52#include "extern.h"
53
54extern SEGUSE *seg_table;
55
56static int check_orphan(struct inodesc *idp);
57
58static int
59check_orphan(struct inodesc *idp)
60{
61	struct zlncnt *zlnp;
62	ino_t inumber = idp->id_number;
63
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;
74		}
75	}
76	return 0;
77}
78
79void
80pass4(void)
81{
82	ino_t inumber;
83	struct zlncnt *zlnp;
84	struct ufs1_dinode *dp;
85	struct inodesc idesc;
86	int n;
87
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]) {
94
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;
109					}
110			}
111			break;
112
113		case DSTATE:
114			clri(&idesc, "UNREF", 1);
115			break;
116
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;
126			}
127			clri(&idesc, "BAD/DUP", 1);
128			break;
129
130		case FCLEAR:
131			if (check_orphan(&idesc))
132				break;
133			clri(&idesc, "BAD/DUP", 1);
134			break;
135
136		case USTATE:
137			break;
138
139		default:
140			err(EEXIT, "BAD STATE %d FOR INODE I=%llu\n",
141			    statemap[inumber], (unsigned long long)inumber);
142		}
143	}
144}
145
146int
147pass4check(struct inodesc * idesc)
148{
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;
155
156	sn = dtosn(fs, blkno);
157	for (ndblks = 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;
169			}
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--;
178			}
179		}
180	}
181	return (res);
182}
183