pass1b.c revision 1.20
1/*	$OpenBSD: pass1b.c,v 1.20 2015/01/16 06:39:57 deraadt Exp $	*/
2/*	$NetBSD: pass1b.c,v 1.10 1996/09/23 16:18:37 christos Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1986, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>	/* MAXFRAG */
34#include <sys/time.h>
35#include <ufs/ufs/dinode.h>
36#include <ufs/ffs/fs.h>
37
38#include <stdio.h>
39#include <string.h>
40#include "fsck.h"
41#include "extern.h"
42
43static int	pass1bcheck(struct inodesc *);
44static struct dups *duphead;
45
46static ino_t info_inumber;
47
48static int
49pass1b_info(char *buf, size_t buflen)
50{
51	return (snprintf(buf, buflen, "phase 1b, inode %llu/%llu",
52	    (unsigned long long)info_inumber,
53	    (unsigned long long)sblock.fs_ipg * sblock.fs_ncg) > 0);
54}
55
56void
57pass1b(void)
58{
59	int c, i;
60	union dinode *dp;
61	struct inodesc idesc;
62	ino_t inumber;
63
64	memset(&idesc, 0, sizeof(struct inodesc));
65	idesc.id_type = ADDR;
66	idesc.id_func = pass1bcheck;
67	duphead = duplist;
68	inumber = 0;
69	info_fn = pass1b_info;
70	for (c = 0; c < sblock.fs_ncg; c++) {
71		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
72			info_inumber = inumber;
73			if (inumber < ROOTINO)
74				continue;
75			dp = ginode(inumber);
76			if (dp == NULL)
77				continue;
78			idesc.id_number = inumber;
79			if (GET_ISTATE(inumber) != USTATE &&
80			    (ckinode(dp, &idesc) & STOP))
81				return;
82		}
83	}
84	info_fn = NULL;
85}
86
87static int
88pass1bcheck(struct inodesc *idesc)
89{
90	struct dups *dlp;
91	int nfrags, res = KEEPON;
92	daddr_t blkno = idesc->id_blkno;
93
94	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
95		if (chkrange(blkno, 1))
96			res = SKIP;
97		for (dlp = duphead; dlp; dlp = dlp->next) {
98			if (dlp->dup == blkno) {
99				blkerror(idesc->id_number, "DUP", blkno);
100				dlp->dup = duphead->dup;
101				duphead->dup = blkno;
102				duphead = duphead->next;
103			}
104			if (dlp == muldup)
105				break;
106		}
107		if (muldup == 0 || duphead == muldup->next)
108			return (STOP);
109	}
110	return (res);
111}
112