summaryrefslogtreecommitdiff
path: root/sbin/dump/ffs_inode.c
blob: 042223157513d4a851fdd570300a2bd87b163757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*	$NetBSD: ffs_inode.c,v 1.10 2001/12/23 12:54:53 lukem Exp $ */

/*-
 * Copyright (c) 1980, 1991, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
	The Regents of the University of California.  All rights reserved.\n");
#endif /* not lint */

#ifndef lint
__RCSID("$NetBSD: ffs_inode.c,v 1.10 2001/12/23 12:54:53 lukem Exp $");
#endif /* not lint */

#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>

#include <protocols/dumprestore.h>

#include <ctype.h>
#include <errno.h>
#include <fts.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "dump.h"

#ifndef SBOFF
#define SBOFF (SBLOCK * DEV_BSIZE)
#endif

struct fs *sblock;

/*
 * Read the superblock from disk, and check its magic number.
 * Determine whether byte-swapping needs to be done on this file system.
 */
int
fs_read_sblock(char *superblock)
{
	int ns = 0;

	sblock = (struct fs *)superblock;
	rawread(SBOFF, (char *) superblock, SBSIZE);
	if (sblock->fs_magic != FS_MAGIC) {
		if (sblock->fs_magic == bswap32(FS_MAGIC)) {
			ffs_sb_swap(sblock, sblock);
			ns = 1;
		} else
			quit("bad sblock magic number\n");
	}
	return ns;
}

/*
 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
 * variables.
 */
struct ufsi *
fs_parametrize(void)
{
	static struct ufsi ufsi;

#ifdef FS_44INODEFMT
	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
	} else {
		/*
		 * Determine parameters for older file systems. From
		 *	/sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat()
		 *
		 * XXX: not sure if other variables (fs_npsect, fs_interleave,
		 * fs_nrpos, fs_maxfilesize) need to be fudged too.
		 */
		sblock->fs_qbmask = ~sblock->fs_bmask;
		sblock->fs_qfmask = ~sblock->fs_fmask;
	}
#endif

	/* Fill out ufsi struct */
	ufsi.ufs_dsize = fsbtodb(sblock,sblock->fs_size);
	ufsi.ufs_bsize = sblock->fs_bsize;
	ufsi.ufs_bshift = sblock->fs_bshift;
	ufsi.ufs_fsize = sblock->fs_fsize;
	ufsi.ufs_frag = sblock->fs_frag;
	ufsi.ufs_fsatoda = sblock->fs_fsbtodb;
	ufsi.ufs_nindir = sblock->fs_nindir;
	ufsi.ufs_inopb = sblock->fs_inopb;
	ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen;
	ufsi.ufs_bmask = sblock->fs_bmask;
	ufsi.ufs_fmask = sblock->fs_fmask;
	ufsi.ufs_qbmask = sblock->fs_qbmask;
	ufsi.ufs_qfmask = sblock->fs_qfmask;

	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);

	return &ufsi;
}

ino_t
fs_maxino(void)
{

	return sblock->fs_ipg * sblock->fs_ncg;
}

struct dinode *
getino(ino_t inum)
{
	static daddr_t minino, maxino;
	static struct dinode inoblock[MAXINOPB];
	int i;

	curino = inum;
	if (inum >= minino && inum < maxino)
		return (&inoblock[inum - minino]);
	bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock,
	    (int)ufsib->ufs_bsize);
	if (needswap)
		for (i = 0; i < MAXINOPB; i++)
			ffs_dinode_swap(&inoblock[i], &inoblock[i]);
	minino = inum - (inum % INOPB(sblock));
	maxino = minino + INOPB(sblock);
	return (&inoblock[inum - minino]);
}