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