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